Getting variable from instantiated clone instead of prefab C#
Here's the problem. I want to get variables from cloned object script, but instead i get variables of prefab's script. Code:
public GameObject prefab;
public Script script;
public int value;
void Start () {
GameObject clone =(GameObject)Instantiate (prefab, transform.position, transform.rotation);
script=(Script)clone.GetComponent<Script>();
value=script.variable;
}
Editor indicates, that script indicates as [Prefab(Clone) (Script)], that means it refered to cloned object.
Hi Teh_$$anonymous$$eeper , Can you explain in detail?? Below are the 3 ways of accessing variables across scripts 1. make a singleton of the class and access them across al the scripts 2. make the required variable static and use it in different scripts 3. other way is the way that you have used it in your example. Explain it in detail so that other developers can easily understand your question and can give you solution in no time. Hope this may help you.NS$$anonymous$$S
The prefab script at start generates random values of variables. Then, after some time, another object, lets call it Spawner, instantiates prefab's clones with anther random values, and by this point it goes fine (cheked it in Editor). But when it comes to copy clones' variables to Spawner buffer, buffer receives original prefab's variables, that been generated at start, ins$$anonymous$$d of clones', that been generated later.
So you mean that, you set a random value in the start of the prefab (and the clone should then also have another random value set in the start) but when reading it's value in the code above, the value is still that of the prefab ins$$anonymous$$d of the clone????
If not, ignore this :p
The code you have above instantiates a clone and then immediatly retrieves the variable. When creating a clone you create a copy of the prefab. With all values identical to the prefab at start.
I think that your problem is that Start is called just before the first frame update(read next, as the current frame has already started). Which means that if you read something from it in the same frame that you instantiated it, Start hasn't been called yet and you get the old values. To fix that you could use Awake ins$$anonymous$$d of Start.
If this was indeed the problem, please reply so that I can convert this into an answer
Sry, it didn't help. Also, i made a mistake. It's Update ins$$anonymous$$d of Start, and part of the code looks like that:
'void Update () {
if(trigger){
GameObject clone =(GameObject)Instantiate (prefab, transform.position, transform.rotation);
script=(Script)clone.GetComponent
Answer by ComradeVanti · Mar 16, 2016 at 10:19 AM
Well mabye you get the correct variables but they just have the same value on the clone and prefab. Is that a possibility?
No. I'm making 3 different clones simultaneously, and values inside is different in all clones and prefab.
Alright. Could you give a more detailed discription on what you are trying to achieve? I mean really discribe the situation. What kind of variables are we talking about here? Why do you need random values each time you instantiate a gameobject. If you give us more info mabye we can find a workaround
Yes -- the code show is getting the value from the Instantiated object. The error would be using prefab.GetComponent(script), but the code shown correctly uses clone.GetComponent.
There's something else wrong, in code not being shown, that's supposed to do something not being described what it is. I don't think anyone can help with that in this Q.
Answer by giorashc · Mar 16, 2016 at 05:46 PM
When instantiating an instance from a prefab the instance variables will have the same values as the prefab. You will have to set the variables on the clone after it has been created via your script.
Your answer
Follow this Question
Related Questions
Edit variable in instantiated clone 1 Answer
For loop and Instantiate issue 2 Answers
How do you make a clone have the same tag as its original? 0 Answers
My health bars image.fillAmount doesnt change. 4 Answers
Bullet clones will not be destroyed. 2 Answers