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.
Ok, it’s obvious why this is not working, I’ll try to explain why:
When you do
var monster : Monster = new Monster();
You might think you are creating a new monster in the scene but you aren’t, the only thing that happens is that a new Monster object is stored in memory. That’s why it can’t find material, because it’s not attached to a gameObject.
What you have to do is make a prefab of your monster, and then do something like this:
var monsterPrefab : Transform;
function SpawnMonster(position : Vector3)
{
Instantiate(monsterPrefab, position, Quaternion.identity);
}
So then you have a function to add new monsters. This code should be added in some kind of MonsterManager class that deals with creating new monsters.
Thanks for the reply. I had a suspicion that this might have been the issue. However, I really want my class to JUST store data including the material and want to avoid Instantiation if possible. Right now it can store integers and floats properly and I can access them quite easily but the material just won’t initialise for some reason. Is there another workaround,or am I going to have to instantiate numerous Empty Objects to store this data?
I don’t really understand what you’re trying to do, so you want to be able to create monsters through script, right? You want to be able to say “new Monster()” and then have a new monster appear in the scene or how should it work? In which script did you write the “var monster : Monster = new Monster();” code?
I don’t want the monster to appear in my scene. I have a plane in my scene and I want to be able to dynamically change the material on the plane to represent a monster. So I don’t need to instantiate a monster. I just want to store a material in monster (and other data such as health and defence - which I’ve already done and can access using monster.health and monster.defence respectively). As long as I can initialise the material field in monster, I’ll be able to access it - but I can’t.
Aha, then you should do it a bit differently. You don’t need a Monster constructor (not even a class declaration but I’m sure you can leave that like it is now). Just add the Monster script as a component to the plane in your scene. Then from another script you could do:
var monster : Transform;
function Start()
{
print(monster.GetComponent(Monster).health);
print(monster.GetComponent(Monster).defense);
print(monster.GetComponent(Monster).material);
}
The script above should be part of some kind of Manager script that controls the state of the monster.
I hope it will help you a bit further, it’s a bit difficult to explain exactly what you should do cause I don’t know how the rest of your game is set up ^^’