Hello, I’m attempting to make a GameObject prefab instantiate when a certain object is destroyed, however, I need to be able to alter the variables instantly upon creating it, my issue comes when I’m trying to access the variables, I don’t have a clue how to do this on instantiated objects, here is all the needed base code in the function:
Presumably you have some script on your instantiated object? Maybe a ‘Thing’ script. So you would do:
void HitByRay()
{
GameObject go = (GameObject)Instantiate (thingamabob, transform.position, transform.rotation);
//get the thing component on your instantiated object
Thing mything = go.GetComponent<Thing>();
//set a member variable (must be PUBLIC)
mything.somepublicmember = 10;
//call a member function (must be PUBLIC)
mything.somememberfunction();
//maybe access one of the built in components (such as a rigid body if it has one)
go.rigidbody.velocity = new Vector3(10,0,0);
//destroy the original object as normal
Destroy (gameObject);
}
FYI: In that code, I’m assuming that whatever script that contains the HitByRay function has a member called thingamabob, which is a GameObject, and has been set (probably in the editor) to reference an existing prefab, which contains a Thing component.