Changing or replace objects

Hello, I have used a script from here: (change or replace objects - Questions & Answers - Unity Discussions)

to replace an Gameobject with another GameObject, this is what I need to begin with but I would like to know how to ‘cycle’ through a series of objects and loop around back to the first object.

For an example I have a room and when clicking on furniture in the room (using a First Person Controller) the furniture will change from ‘chair’ to ‘stool’ to ‘sofa’ etc.

Could anyone help to edit or add to the script to achieve this?

Here is the script I have been using:

var object2 : GameObject;
var object3 : GameObject;
function OnMouseDown ()
{
Instantiate(object3,object.transform.position,object2.transform.rotation);

Destroy(object2);
}

Any help would be greatly appreciated, I have a good knowledge of Unity but less on scripting from scratch.

Thank you, Regards,

Christian

Something like this might give you a direction:

    //This is pseudo-code only and should not be taken as-is!!

    var objects : GameObject[];
	var index : int;
	var currentObject : GameObject;

	function OnMouseDown()
	{
		index++;
		index = Mathf.Repeat(index, objects.Length);

		if(currentObject != null) Destroy(currentObject);

		currentObject = Instantiate(objects[index], spawnPosition, spawnRotation);
	}

The secret is with the Mathf.Repeat call to get the index back to the beginning of the array.

Thank you for the reply Asafsitner, Unity keeps firing back “BCE0005: Unknown identifier: ‘spawnPosition’ & ‘spawnRotation’.”

Do I need to create and empty Game Object named ‘spawnPoint’ to coincide with the script?

I have assigned the script but there are no var slots to drop in my objects for the array, what do you suggest I do to rectify this?

Thank you again.