how to update a specific script from a prefab to the in scene object
i have a script with a integer that changes when a object is destroyed but it affects only my prefab, not the in scene object, how do i update the script so that can affect my object in the scene?
Answer by CapitalLlama · Aug 31, 2016 at 08:10 PM
The simplest way I think is probably to find the object(s) you want to update in the scene. Then send it a message with the new value or how to update their value. If you have multiple objects that you need to update, find them all in an array and loop through it.
//Object that is changing the value
GameObject someobj = GameObject.FindGameObjectWithTag ("someObject");
someobj.SendMessage ("UpdateInt", i);
You then make a function in the object called the same thing as the string in SendMessage. In this case, UpdateInt.
//The object receiving the update
int intToBeChanged = 0;
void UpdateInt(int i){
intToBeChanged = i;
}
Your answer
Follow this Question
Related Questions
Instantiate not spawning at correct position 1 Answer
Sprites assigned to prefab' script' fields not assigned after launch 0 Answers
Bullet not moving rigidbody2d,AddForce not working for bullet 1 Answer
Call function when prefab is collided 1 Answer
Delete prefab clone when collided with the player, Unity 2D 1 Answer