I have instantiated a prefab in the scene that gets destroyed after 2 secs. I want to change its speed variable in its move script so that whenever it instantiate it has new speed.
2 Answers
The instantiate function returns the game object you created.
GameObject prefabObject = Instantiate(...);
Then you can obviously do something like
prefabObject.GetComponent<moveScript>().speed = 4;
3 Comments
gameObject as it is the name of variable used to access the GameObject this script script is attached to.You have not provided enough information, but I will try to answer.
Will this be done by another script or the Move script you have attached on the prefab?
If from the Move script then:
private void Awake()
{
speed = MY_NEW_SPEED;
}
If from another script, then you first need to access the instantiated object and then its Move scripts. You have not provided enough information so I will assume you already have a reference to your instantiated object:
instantiatedObject.GetComponent<Move>().speed = MY_NEW_SPEED;
https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html