Simple question about affecting variables...

Hi. I know this is simple, but for some reason I cannot get it to work. I want to search my scene for the object named “MyObject” with the script “Cell” attached to it and change the value of the variable “TEST”. For some reason I get this error “No appropriate version of ‘UnityEngine.GameObject.GetComponent’ for the argument list ‘(int)’ was found”…what am I doing wrong (I have been able this to work before, but I must have messed something up…and I cannot figure out what)? Thanks

GameObject.Find("MyObject").GetComponent(Cell).TEST=10;

If we look at the syntax for GetComponent() we see that it wants a parameter which is a “Type”. The error is saying that you are passing in a parameter of type “int”. There could be a number of reasons for this. Maybe you have a variable called “Cell”, maybe that has become a “reserved word” in Unity or .NET.

Either way, there is an alternative syntax for GetComponent() which takes a String parameter. Try coding the following:

GameObject.Find("MyObject").GetComponent("Cell").TEST=10;

Note the quotes around the GetComponent parameter. If this still does not work, break the command up into its constituent parts to find the exact place the error occurs:

var go1 = GameObject.Find("MyObject");
var co1 = go1.GetComponent("Cell") as Cell;
var co1.TEST = 10;