visit from an UGLY fairy!

Hello all…

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.

Okay, it’s nicer (as in, nicer than it’s ugly, older sister) and it still works. But does give me an error:

var arr : Texture2D[]; 
var no = 0;
var n = gameObject;
var e = gameObject;
var s = gameObject;
var w = gameObject;
var arrows = gameObject;

this.gameObject.renderer.enabled = false;

function OnGUI () {
	
	if (GUI.Button (Rect (10,Screen.height - 30, 80, 20), "SLIDE")) {	
		this.gameObject.renderer.enabled = true;
		arrows.renderer.enabled = false;
		n.renderer.enabled = false;
		s.renderer.enabled = false;
		e.renderer.enabled = false;
		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;
		arrows.renderer.enabled = true;
		n.renderer.enabled = true;
		e.renderer.enabled = true;
		s.renderer.enabled = true;
		w.renderer.enabled = true;
	}
}

[/quote]

Does that exception say what line it is caused by? Does double-clicking on the exception bring you too the line that is causing it?

Was that exception there in the previous version, or after you made the changes?

Hi Tempest

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.

Thanks for your help.

var n = gameObject;

should be

var n : GameObject;

Same result… Same error message.
The script still works however.
:shock:

The script will still work, because until all the errors go away, it’s still using the old, ugly script.

Post the full script, along with what lines exactly are giving you the errors.

You should do:

var compassObjects : Renderer[];

function OnGUI () {
   ...
      for (object in compassObjects) {
          object.enabled = false;
      }
   ...
}

–Eric

Thanks Eric5h5 and Tempest.

That worked.

Much neater now.

All the best.