- Home /
How to change a variable on the script of an instance that is one of many
Hi,
I have a prefab that is repeated many times to make many instances. It has a script on it with a variable. I can access the instances individually and I know how to call to a function in another script to change the variable in it.
My understanding is that by using the command (after the script is found and thisScript has it assigned to it):
thisScript.thisScriptFunction (variable);
I will end up triggering thisScriptFunction and changing the variable on every instance that has the script on it. If I want to only trigger the change on one instance of the many how do I specify in the command?
for example:
thisInstance.faceScript.FaceChangeCount (groupTrackerH);
does not work.
Thank you for any help!
I converted your response into a comment as you should only post as an answer if you are offering an answer to the question. However for some reason whilst the hide/show button suggests the comment is there It isn't rendering for me, so my apologies if I've done that wrong and it's broken the comment. But I've updated my answer to include some code that hopefully will help explain what you are trying to do.
Could you give more details of the game itself? it would help a lot to have some understanding of what you actually want and think of a solution, your question is too vague.
Answer by HenryStrattonFW · Jan 18, 2017 at 11:46 PM
Your statement that calling the function on the found item effects all instances is not correct (unless I misunderstand you). This would only be true if the function you are calling is static to the type, or if the variable that function is modifying is static to the type.
If you have run your find method and have a reference to a single instance of an object, then calling methods on it (unless static) should only effect that single instance (unless as previously mentioned that function modifies static variables on the type).
It would be easier to get a clear idea of what your issue is / what you are trying if you could provide some code to refer to.
EDIT: Ok so I've written two monobehaviours that should demonstrate the idea of how to set a variable on an instance of a script individually.
public class OneOfMany : MonoBehaviour
{
private string message = "none";
public void SetMessage(string newMessage)
{
message = newMessage;
}
}
public class Test : MonoBehaviour
{
public void SetMessages ()
{
OneOfMany[] many = GameObject.FindObjectsOfType<OneOfMany>();
for(int i=0;i<many.Length;i++)
{
many[i].SetMessage("I was number: "+i.ToString());
}
}
}
You can apply the OneOfMany script to multiple objects in your scene, then if you add Test to one object and then ran the SetMessages method, it finds ALL of the OneOfMany scripts in the scene, and sets the message of each one which will then be unique.
Great, thank you!
If I have the following code to gather up all the magenta faces and their corresponding scripts:
public class Test : $$anonymous$$onoBehaviour
{
private GameObject[] all$$anonymous$$aj;
private GameObject[] all$$anonymous$$ajScripts;
private FaceScript[] faceScriptArray;
void Start ()
{
faceScriptArray = GameObject.FindObjectsOfType<FaceScript>();
}
void Check$$anonymous$$ajenta ()
{
int m = 0;
for (int f = 0; f < faces.Length; f++)
{
if (faces[f].activeSelf)
{
if (faces[f].tag == "$$anonymous$$aj Face")
{
all$$anonymous$$aj[m] = faces[f];
all$$anonymous$$ajScripts[m] = faceScriptArray[f];
m++;
}
}
}
}
}
Question 1 is that I get an error saying, "Cannot implicitly convert type 'FaceScript' to 'UnityEngine.GameObject'." What should it be?
Question 2 is will the instance and its script be on the same number in their arrays so that I can say, if its face number 4 then set the variable of script number 4 to whatever?
Thank you again!
"Cannot implicitly convert type 'FaceScript' to 'UnityEngine.GameObject'." because faceScriptArray is of object type FaceScript and all$$anonymous$$ajScripts is of type GameObject ...
all$$anonymous$$ajScripts[m] = faceScriptsArray[f].gameObject;