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
1
Question by Novrick · Oct 19, 2011 at 09:35 AM · destroyobjectscreate

destroy and create objects

Hey, I have made a script attached to a box collider that is positioned in front of the player. With the script I have so far made it possible for the player to create objects with the mouse buttons.

but I have no idea how to make that the player can also destroy objects that are infront of him/her.

EDIT: I've updated the script (the last code section) with a solution that is (what I believe) to be the right direction. but something is missing, anyone of you who knows what?

here is the script:

 using UnityEngine;
 using System.Collections;

 public class spawnscript : MonoBehaviour {
 
 
 public GameObject cubeprefab;
 public GameObject rectangleprefab;
 public GameObject stairprefab;
 public GameObject spawncube;
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     
     
             
     if(Input.GetKeyDown("mouse 0"))
     {
         
         if(idea.Instance.shadow1 == true)
         {
         
     //Vector3 position = new Vector3(camera.transform.position.x + 2, camera.transform.position.y);
     
     Instantiate(cubeprefab, transform.position, transform.rotation);
     //Physics.IgnoreCollision(cubeprefab.collider, spawncube.collider);
         }
         
     }
     
     
     if(Input.GetKeyDown("mouse 1"))
     {
     
     if(idea.Instance.shadow2 == true)
         {
         
     Instantiate(rectangleprefab, transform.position, transform.rotation);    
             
         }
         
     }
         
         
         
     if(Input.GetKeyDown("mouse 2"))
         {
         
             if(idea.Instance.shadow3 == true)
             {
                 
             Instantiate(stairprefab, transform.position, transform.rotation);
                 
             }
             
             
             
         }



     if(Input.GetMouseButtonUp(0))
     {
     Vector3 fwd = transform.TransformDirection(Vector3.forward);
     RaycastHit hit;
     if(Physics.Raycast(transform.position,fwd,10))
         {
             
         Destroy(hit.transform.gameObject);    
             
         }
         
     }
         
         
     }
     
     
     
     
 
 }
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

1 Reply

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

Answer by gfr · Oct 19, 2011 at 11:24 AM

If your objects already have colliders, you could use e.g. `OnMouseUpAsButton()` on them and destroy it from there. Alternatively you could raycast on mouseclick and if you hit a destructible object, remove it instead of creating a new one. E.g.:

 if(Input.GetMouseButtonUp(0)) 
 {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 100)) {
         Destroy(hit.transform.gameObject);
     } else {
         // instantiate new one
     }
 // ...

To restrict what can be destroyed use layer masks for your raycasts or add additional conditions like checking for tags etc.

Comment
Add comment · Show 10 · 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 Novrick · Oct 19, 2011 at 12:01 PM 0
Share

how would you code if you'll try with the raycast solution? cuase I want to be able to destroy anything with a rigid body

avatar image gfr · Oct 19, 2011 at 06:43 PM 0
Share

Updated with some basic example.

avatar image Novrick · Oct 20, 2011 at 11:27 AM 0
Share

oh! forgot to mention that I'm working in C sharp ^^''

avatar image gfr · Oct 20, 2011 at 01:56 PM 0
Share

Well, happy translating then ;)

avatar image syclamoth · Oct 22, 2011 at 10:27 AM 1
Share

if you're using C#, you need to put the keyword 'out' before hit. So,

 Physics.Raycast(transform.position, fwd, out hit, 10);
Show more comments

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

objects not reappearing 2 Answers

Should I destroy and create objects from prefabs to create a "copying" gameplay mechanic? 0 Answers

How to activate an object when another object deactivates? 1 Answer

Destroy All Objects In The Scene??? 1 Answer

How to iterate all prefabs in project and create them in scene 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