- Home /
Changing or replace objects
Hello, I have used a script from here: (http://answers.unity3d.com/questions/22651/change-or-replace-objects.html)
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
Answer by asafsitner · May 15, 2012 at 12:48 PM
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.
Answer by csavenables · May 15, 2012 at 03:14 PM
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.
First, there's no need to add a whole new answer if you have issues or more questions - that's what the 'add new comment' button is for. :)
Second, the code above was written more as a pseudo-code to give you a direction rather than an actual working code, since I don't know what exactly you're trying to do and where you want to instantiate the objects and whether or not you want to instantiate multiple copies of the same object and switch objects with the mouse wheel, for instance.
I'm sorry, I should have made it clear and will rectify that (and a $$anonymous$$or error with the Repeat
call).
Your advice is very helpful, I'm learning fast :)
I would to walk up to an object in my scene (first person controller), click on a sphere for arguments sake and a new object/cube will appear in it's place, then if this is clicked another new object/cylinder will appear. Finally I would like to click on the cylinder and it will loop around back to the sphere, it's key this cycle continues. I have been trying to use Array's for this, I have successfully created a material array (from pieces of acquired script) but I have not been as successful for Object Arrays.
I hope this is clear, this is what I have in the way of script to begin with:
var arr : GameObject[];
private var index : int;
function On$$anonymous$$ouseUp ()
Your help will be greatly appreciated :)