- Home /
Can't access the script of an instantiated prefab :(
I have this code that works fine:
public class Main : MonoBehaviour {
public Human prefab;
void Start(){
Human human = (Human)Instantiate (prefab);
}
}
It creates a human that walks around and does what he should. What's weird is that "Human" is the name of the class, not the prefab. What I'd like to do is alter some properties in this script. But if I try:
human.speed = 5;
...I get a CS1061 error about it not containing definitions. I've looked this up on the forums and in the manual, and there seems to be clear answers, but when I implement them it doesn't work. I think I'm still missing something core here... I tried using getComponent() to get the script, but that doesn't work either. Any ideas on what I'm missing here? Thanks!
BTW, this is the GetComponent script I tried.
Human c = human.GetComponent<Human>();
c.charName = "gabe";
It showed the properties fine as I typed c. but it still did not set the value when I ran it. I'm doing something wrong...
Answer by fafase · Dec 20, 2012 at 08:28 AM
This might be a wrong use of static variables. Do you plan on having many Human objects at the same time? If so, static won't do as all of them would get the same unique speed.
I thought static meant that it was unchangeable
Nope, this is const or readonly. You could have an issue with naming though. From what I see, you are mixing prefab reference and object reference
Human.cs
public class Human : MonoBehaviour {
public int speed = 5;
}
TheOtherScript.cs
public class NewClass:MonoBehaviour {
public GameObject prefab; // Drag prefab here from editor
GameObject human;
void Start(){
human = (GameObject)Instantiate(prefab, new Vector3(0,0,0), Quaternion.identity);
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
Human script = human.GetComponent<Human>();
script.speed=10;
}
}
}
Fact is the prefab declaration is a reference to a prefab in the project panel. This is not what you need to access. Also your Human declaration is within the Start, so it gets lost at the end of it. When using human you were talking to the prefab but not the object. Instantiate returns an Object that you need to cast into a GameObject to have access to the functionality of a game object.
Thanks, fafase! This was extremely helpful and corrected some things I was thinking about all wrong. One thing I'm still having trouble with is why I shouldn't set my speed, etc in the start(). I basically just want to initialize this... like set it's name, etc. I know there's other ways and places to do that, but is there a reason NOT to do it in the start()?
In the Start or as I did is pretty much the same. From what I know that might be wrong, you cannot have an explicit constructor with classes derived from $$anonymous$$onoBehavior. When using
int variable =10;
you somehow initialize in the constructor that Unity makes. Start is a function that is called. So you can perform some more action like for loops or used values of other variables.All in all not a big difference I guess. Awake and Start also define when you wan tit to happen. To be confirmed
@eric5h5 will probably answer that better than me though...
Answer by magicqy · Dec 21, 2012 at 12:58 AM
you should Instantiate a GameObeject with a script named "Human" not to Instantiate a script only.
This is wrong. There's no problem using a script name as a public variable and then instantiating that. All it means is that you drag a prefab which contains that script onto the slot and it instantiates that prefab. So you get the GameObject with the script attached as an instance. In the exact same way that you can specify any other component as a public variable and instantiate it.
Your answer
