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 netherboss123 · Mar 30, 2015 at 09:12 AM · buttoncarswitch

How To destroy specific objects?

Hello! i am making a racing game where you can change your car anywhere by pressing "C" and choosing a car. i have a code that should do that but i dont want you to have more than 1 car at a time. is there a way to destroy all other cars when you spawn a new one? i have tried to do

 Destroy (HoverCar);

or

 Destroy (Truck);

but it doesnt work. please could somebody tell me what i did wrong?

code: var HoverCar : Transform; var Truck : Transform; var script : Camera;

 function OnGUI () { 
 
     if (Input.GetAxis("C")){
             
             if (GUI.Button(Rect(10,70,100,30),"Hover Car")){
             Destroy (HoverCar);
             Destroy (Truck);
                Instantiate (HoverCar, Vector3 (542.78, 1.3, 312.64), Quaternion.identity);
             script = GetComponent(Camera); script.enabled = false;
             }
             
             if (GUI.Button(Rect(10,100,100,30),"Truck")){    
             Destroy (Truck); 
             Destroy (HoverCar);     
             Instantiate(Truck, Vector3 (542.78, 1.3, 312.64), Quaternion.identity);
             script = GetComponent(Camera); script.enabled = false;
             }
             
         }
     }

Comment
Add comment · Show 2
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 Graham-Dunnett ♦♦ · Mar 30, 2015 at 09:13 AM 0
Share

What does "it doesn't work" mean?

avatar image AeonIxion · Mar 31, 2015 at 07:24 AM 0
Share

Does the rest of that code work? eg does it create a button and instantiate a different car? Because I think you need Input.Get$$anonymous$$ey("c") ins$$anonymous$$d of Input.GetAxis("c")

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ShabihDesperado · Mar 31, 2015 at 07:39 AM

HoverCar and Truck are transforms? Well, I think that you need to save the GameObject that you Instantiate and destroy this GameObject, then Instantiate a prefab. What I'm watching in your code is that you are destroying the same reference you are gonna instantiate.

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 KdRWaylander · Mar 31, 2015 at 10:22 AM

Hi :)

There are different solutions but here is the one that comes to me:

  • Add a "car" tag to your cars/trucks

  • Find all the the cars with GameObject.FindGameObjectsWithTag (http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html) that will give an array of all the cars/trucs with the good tag

  • Destroy the objects in the array

  • Instantiate your new car

  • Have fun and drive carefully

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I switch paths by pressing a button? 1 Answer

Car game Reverse Button 1 Answer

On mouse down switch on / off 2 Answers

Reverse Button problem 0 Answers

Weighing down a heavy switch 2 Answers


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