Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by csavenables · May 15, 2012 at 11:08 AM · gameobjectchangereplace

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asafsitner · May 15, 2012 at 05:41 PM 0
Share

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).

avatar image csavenables · May 17, 2012 at 09:24 AM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Modifying several gameobjects with an editor script? 2 Answers

Changing gameobject assigned to a variable from string 1 Answer

script help gameobject to playercar 1 Answer

How to change part of complex gameobject instantiated from prefab? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges