I have been looking everywhere, and haven’t found what I am looking for. I could also just be using the wrong words for the question… but anyway here’s my dilemma.
I am using both script (JS) and the Button>OnClick Component for what I am attempting to do.
On my cursor object I have one Game Object var and a Game Object list var.
(and here is my script)
var selectedPre : GameObject;
public var objects : GameObject[];
function Update () {
if (Input.GetKeyDown(KeyCode.Mouse0)) {
Instantiate(selectedPre, Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity);
}
}
My question is, how would I go about swapping out the prefab on “SelectedPre” for one of the prefabs in the “objects” list on button click?
Not 100% sure I understand the question… but something like this…
..
Instantiate(objects[Random.Range(0, objects.Length)], Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity);
..
Or maybe…
var selectedPre : GameObject;
public var objects : GameObject[];
function Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var randSelect : int = Random.Range(0, objects.Length);
selectedPre = objects[randSelect];
Instantiate(selectedPre, Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity);
}
}
Oh, sorry. To clarify: I have a UI Element with buttons, and when clicking on “Button1” I would like selectedPre to swap to “Object1”, or click on “Button2” have it swap to “Object2.” Thanks for the quick response, by the way.
Okay so this is kind of hacky but it should work. So you are using a button with the built in OnClick() functionality in the Inspector?
I would make a manager object which stores this script. Then from each button under the OnClick() field in the Inspector select whatever manager object you chose which contains the script, let’s say you named the object Button_Manager then select the function and type in the value into the inspector; so button one would be 1, button two would be 2, etc etc.
So the code for the manager would look something like this:
var selectedPre : GameObject;
public var objects : GameObject[];
function buttonClicker(_val : int)
{
selectedPre = objects(_val - 1);
}
The subtraction of 1 is assuming the array is index of 0. 
I put together a demo project with this code in action!!! Take a look! Hope this helps!
https://www.dropbox.com/s/ck71l8wmy5ixhah/Button_Stuff.zip?dl=0
That helps loads. Thank You.
No problem! Glad to help!! 