passing info/getting info

Ok, after exhaustive searching, I still haven’t come up with an answer, which must be extremely obvious and easy, so apologies…

Two codes:
One is a gui which controls the material of the selected object.
At the moment all the objects, when selected go through the same array of Materials. However, I need each selected object to have it’s own array of Materials. To this end I have placed an array of materials on each object, with this code, called “TextureArray”

var textures : Material[];

How do I get the following script to access the above array on the selected object?
At the moment, when an object is clicked it sets itself as “selected” in this code…“MapControl”

static var selected : GameObject;
var arr : Material[];
var no = 0;

function OnGUI () {
	 	
if (GUI.Button (Rect (615,Screen.height - 30, 30, 30), ">>")) {
    no += 1;
    if (no >= arr.length) { no = 0; }
    selected.renderer.material = arr[no]; 

}  
   if (GUI.Button (Rect (540,Screen.height - 30, 30, 30), "<<")) {
    no -= 1;
    if (no <=- arr.length) { no = 0; }
    selected.renderer.material = arr[no];

}
	GUI.Label(Rect(580,Screen.height - 20, 40, 30), "MAP");
}

Thank you.[/code]

Are you sure about the gameobject set it self as selected?

I do think that the code line

selected.renderer.material = arr[no];

should set the material. Have you tried using:

selected.renderer.enabled = false;

Just to make sure that the object is selected?

Hi Eriksen

Yes, if I change the line to renderer.enabled = false, my selected objects disappear, so it seems to work. The materials from that array work as well, it’s just not the array that I want. I want to set the material array to an array which is attached to an individual object.

Thanks for your help.

I’m not totally sure that i understand what you want.

but if you want to copy the array from the selected gameobject then you should be able to do that using:

var other : ScriptName = selected.GetComponent(ScriptName);

other.arr[no];

Thanks Eriksen

I couldn’t get it to work. Perhaps I can state the problem a little clearer.

I have two game objects. Each object has a TexturesArray.js attached to it, which goes like this: var textures : Material[];

Then the following code, called MapControl.js is attached to an empty game object in the scene:

static var selected : GameObject; 
var arr : Material[]; 
var no = 0; 

function OnGUI () { 
        
if (GUI.Button (Rect (615,Screen.height - 30, 30, 30), ">>")) { 
    no += 1; 
    if (no >= arr.length) { no = 0; } 
    selected.renderer.material = arr[no]; 

}  
   if (GUI.Button (Rect (540,Screen.height - 30, 30, 30), "<<")) { 
    no -= 1; 
    if (no <=- arr.length) { no = 0; } 
    selected.renderer.material = arr[no]; 

} 
   GUI.Label(Rect(580,Screen.height - 20, 40, 30), "MAP"); 
}

When I click on an object, it becomes the selected object.

The above code works by itself and I can select an object and cycle through the materials in the array contained in the empty game object.

However the reason I have TexturesArray script attached to both objects, is because each array contains different materials and I would like the selected object to only access it’s own array, not share an array, which the objects are doing currently.

Perhaps there is an easier/better way of doing this.

All I need is two GUI buttons, which when pressed would change the material on the selected object to A,B,C… Then when another object is selected, the same buttons would change the selected material to 1,2,3… Depending on what is in it’s material array.

Sorry for the long winded response, but it’s the only way I can explain it.

Thanks

Hey again

I do normally not program in javascript. So i’m not sure about this, but i think this will work:

static var selected : GameObject; 
var no = 0; 

function OnGUI () { 

var other : TexturesArray = selected.GetComponent(TexturesArray); 

        
if (GUI.Button (Rect (615,Screen.height - 30, 30, 30), ">>")) { 
    no += 1; 
    if (no >= other.textures.length) { no = 0; } 
    selected.renderer.material = other.textures[no]; 

}  
   if (GUI.Button (Rect (540,Screen.height - 30, 30, 30), "<<")) { 
    no -= 1; 
    if (no <=- other.textures.length) { no = 0; } 
    selected.renderer.material = other.textures[no]; 

} 
   GUI.Label(Rect(580,Screen.height - 20, 40, 30), "MAP"); 
}
public var textures : Material[];

THANK YOU!!! :smile:

Just tried it and it works brilliantly!

Much respect!

Eriksen, people like you make this community what it is!

Thanks again.

ohh now your just making me blush :sweat_smile: