- Home /
Cannot Assign Material To Object
Hi guys, I'm having a problem with assigning a material to an Object which is an instance of a class. In the Inspector, I've dragged the Material I want but when I print out the value of the material, it gives me null.
public class Monster extends MonoBehaviour {
var material : Material;
//Constructor
public function Monster() {
print(material); //This gives null even though the material is clearly assigned in the Inspector
}
}
//I call this constructor using:
var monster : Monster = new Monster();
I'm completely stumped on this so any help is much appreciated.
how are you assigning a material in the editor for a class that you are instantiating at runtime?
I've created a prefab for the class just so I can assign a material in the inspector. I have a feeling that the problem arises due to the fact that even though I'm calling the constructor, I don't actually make an instance of the $$anonymous$$onster. Somehow I CAN still retrieve all the other fields such as health or defence of the $$anonymous$$onster class but the material field just doesn't want to be initialised, thus always returning a null value.
Answer by sriram90 · May 16, 2014 at 10:02 AM
you shouldn't implement (and definitely shouldn't call) constructors for MonoBehaviours. One of the reasons for this is that in the best-case scenario, your constructor will be called when the game starts, and then the contents of the instance will be over-written by de-serialized data as Unity loads up your scene file.
If you want initialization code to run when the game starts, use Start() or Awake(). Awake() will be called immediately as part of the instantiation process. Start() will be called before the first Update() call that the newly instantiated object gets.
ScriptableObjects are non-Behaviour Objects that Unity lets you serialize as stand-alone assets. They're for storing data that doesn't belong to a specific level or prefab. GUISkins are a good example of a ScriptableObject.
Source : Check here for better explanation
Honestly, the only reason I've even got the class extending $$anonymous$$onoBehaviour is because I can't otherwise access the Inspector to drag and drop the material that I want to use for that $$anonymous$$onster. I don't actually want to Instantiate the $$anonymous$$onster prefab, rather I just want it to store a bunch of fields for the stats of the monster and a material (which includes a texture) so I can recognise the monster.
You just need to avoid constructor in monobehaviour. Just keep your code (Whatever you want to do) in start() function.
declare the variable as public, you will see in inspector! and dont use Constructor and use Awake or start which is the equivalent for Constructors for monobehaviour!
Your answer
Follow this Question
Related Questions
Assign Material by script: Javascript? 1 Answer
A surface that has multiple physical materials? 0 Answers
Move object to raycast point. 3 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Random instantiation at an object 1 Answer