-3

I've tried multiple ways of initializing the object, and the thing just wont work. I think the problem may lie in one of my other classes, so I've put all the important ones in pastebin.

THE CLASS THROWING THE ERROR: http://pastebin.com/Pj04kUbj

THE TYPE CLASS OF THE OBJECT THROWING THE ERROR:

public class PlayerCharacter : BaseCharacter {

}

THE SUPERCLASS OF THE OBJECT: http://pastebin.com/GAmSMXbh

(damn thing wont let me post more than 2 links)

I doubt this will be solved here, as this is kind of a massive infodump, but if its something really simple and you guys catch it that would be awesome. In the meantime, ill keep looking, but im having little luck

LearnCocos2D
  • 63,754
  • 20
  • 125
  • 213
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Feb 16 '15 at 05:49
  • I am afraid it is not a duplicate of that question. I am aware of what the error is, the problem is I shouldn't be getting it, The variable is not set to null, yet it is receving a NullReferenceException – user3703570 Feb 16 '15 at 17:35
  • .NET doesn't throw `NullReferenceException` unless the code is dereferencing a `null` value. Period. In any case, a good question is self-contained, and includes [a _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably demonstrates the problem. A good question does _not_ rely on links to external web sites, nor does it ask readers to try to sift through all of your code (whether posted on an external web site or here on StackOverflow). – Peter Duniho Feb 16 '15 at 17:44
  • I understand that it will not throw a NullReferenceException unless the variable is null. The variable is null. But it shouldn't be, I am asking why it is null. And I apologize for posting to much of my own code to sift through, but I wanted to provide as much info as possible. Looking back, the error is probably central to the class that is throwing the error and the other 2 may not have been needed – user3703570 Feb 16 '15 at 18:02

1 Answers1

0

You cannot create a GameObject in that way:

If you use new operator to create PlayerCharacter, you will not have a Unity GameObject at the end. You must Instantiate from a prefab or clone from another GameObject.

On the other hand, you cannot call Awake directly because the Unity GameObject is not built. This function is called from Unity when the GameObject is built for its first time.

See here for an example of creating GameObjects.

EDIT:

you must add the script component to your PlayerCharacter like this:

_toon.AddComponent( PlayerCharacter ) as PlayerCharacter

in that way the script is attached to your GameObject because using new to create it, the script is not attached.

Jordi Cruzado
  • 770
  • 5
  • 12
  • "You must Instantiate from a prefab or clone from another GameObject." - Incorrect, to create new GameObject simply call new GameObject(string gameObjectName). Reference http://docs.unity3d.com/ScriptReference/GameObject-ctor.html – Greg Lukosek Feb 16 '15 at 09:05
  • Thank you for responding, you are correct, but the issue is that PlayerCharacter is not a gameobject, but unity is still throwing a warning when I use the "new operator". The exact same problem occurs when I use this code instead. http://pastebin.com/64GirrW0 It seems to think that the _toon variable is null, even though I set it to be equal to an instance of the PlayerCharacter Class – user3703570 Feb 16 '15 at 17:31
  • do you receive this message in console when play the game in the editor: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all? – Jordi Cruzado Feb 17 '15 at 08:22
  • See my EDIT in the answer related to the warning in console – Jordi Cruzado Feb 17 '15 at 08:29
  • @Greg Lukosek, you are right. You can use new to create a GameObject but it lacks of the script component – Jordi Cruzado Feb 17 '15 at 08:33