- Home /
How do you create new variables?
How do you create new variables so I can reuse the same variable name. For example I get an error saying newGrid and oldObj is already in use, I like to reuse them. Once Im done testing I might create a if loop for all my 27 grid sizes.
void EditGridColor() {
if (gridSize == 27) {
Transform newGrid = (Transform) Instantiate(gridRed, new Vector3(0, 0, 0), Quaternion.identity);
var oldObj = GameObject.Find("Main Camera/BuildMode/Grid/26");
newGrid.transform.parent = oldObj.transform.parent;
newGrid.transform.rotation = oldObj.transform.rotation;
newGrid.transform.position = oldObj.transform.position;
GameObject.Destroy(oldObj);
newGrid.transform.name = "26";
Transform newGrid = (Transform) Instantiate(gridPurple, new Vector3(0, 0, 0), Quaternion.identity); // newGrid variable is already defined error
var oldObj = GameObject.Find("Main Camera/BuildMode/Grid/25"); // oldObj variable is already defined error
newGrid.transform.parent = oldObj.transform.parent;
newGrid.transform.rotation = oldObj.transform.rotation;
newGrid.transform.position = oldObj.transform.position;
GameObject.Destroy(oldObj);
newGrid.transform.name = "25";
}
}
Answer by xortrox · Apr 21, 2014 at 03:16 PM
Just skip using "var" the second time
ok, that did it, I also removed the Transform.
newGrid = (Transform) Instantiate(gridPurple, new Vector3(0, 0, 0), Quaternion.identity);
oldObj = GameObject.Find("$$anonymous$$ain Camera/Build$$anonymous$$ode/Grid/25");
Thanks for the fast reply. Happy Easter $$anonymous$$onday.
Your answer
Follow this Question
Related Questions
Move objects infinite based on distance and not an invoke 2 Answers
Strategy for reusing scripts on multiple levels 1 Answer
Best Methods for Asset and Code Reuse and Organization 3 Answers
Is there a better way to reuse animators on similar characters 0 Answers
How to go about making and organizing scripts that you only use once. 0 Answers