The code below works, BUT there MUST be a more elegant way of doing this. All I’m trying to do is to turn off a series of objects that make up my compass. It’s just arrows and four letters S,E,N,W. I have tagged all of the above as ‘compass’, but for some reason can’t get FindObjectsWithTag to work, so I’m left with this ugly MOFO. If someone can tidy it up for me, or suggest a better solution I’d be glad to hear it. Thank you all.
var arr : Texture2D[];
var no = 0;
this.gameObject.renderer.enabled = false;
function OnGUI () {
if (GUI.Button (Rect (10,Screen.height - 30, 80, 20), "SLIDE")) {
this.gameObject.renderer.enabled = true;
var compass = GameObject.Find("arrows");
compass.renderer.enabled = false;
var n = GameObject.Find("N");
n.renderer.enabled = false;
var s = GameObject.Find("S");
s.renderer.enabled = false;
var e = GameObject.Find("E");
e.renderer.enabled = false;
var w = GameObject.Find("W");
w.renderer.enabled = false;
}
if (GUI.Button (Rect (100,Screen.height - 30, 30, 20), "<<")) {
no -= 1;
if (no <=- arr.length) { no = 0; }
renderer.material.mainTexture = arr[no];
}
if (GUI.Button (Rect (140,Screen.height - 30, 30, 20), ">>")) {
no += 1;
if (no >= arr.length) { no = 0; }
renderer.material.mainTexture = arr[no];
}
if (GUI.Button (Rect (180,Screen.height - 30, 30, 20), "X")) {
this.gameObject.renderer.enabled = false;
var com = GameObject.Find("arrows");
com.renderer.enabled = true;
var a = GameObject.Find("N");
a.renderer.enabled = true;
var b = GameObject.Find("S");
b.renderer.enabled = true;
var c = GameObject.Find("E");
c.renderer.enabled = true;
var d = GameObject.Find("W");
d.renderer.enabled = true;
}
}
Having the objects as public variables which you can assign in the Inspector will have you the trouble of using GameObject.Find(). That will clean up a lot of that.
It’s the third line, so the var for the first game object. I don’t remember getting an exception prior to the change. Still think this could be a lot neater.