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 whitequill · Jun 08, 2013 at 07:28 AM · errorraycastdestroy

Destorying and respawning objects with raycasting

This code worked as-is just a little while ago. I'm trying to figure out what I did wrong. Did I remove something important without knowing it?

I'm sure it's something obvious in the syntax. It errors saying there is something wrong with line 25, which I know there isn't.

On a side note I am having trouble getting destroy working right Also when the ray hits tag "wall", I want to Instantiate the object I just destroyed. Tried prefabs, but had some issues with, destroying them.

 using UnityEngine;
 using System.Collections;
 
 public class RayCast : MonoBehaviour 
 {
     
     //cast ray
     public GameObject Character;
     private RaycastHit rayHit;
     private GameObject ray;
     
     //destroy wall
 //    public GameObject Wall;
     
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         
         Vector3 myTransform = ray.transform.forward;
         
         if(Physics.Raycast(transform.position, myTransform, out rayHit, 10))
         {
             if(ray.collider.gameObject.CompareTag("red"))
 //                Debug.DrawRay(transform.position, Character.transform.forward, Color.red);
                 print("Destroy Wall!");
 //            Destroy(Wall, 0);
             
             if(rayHit.collider.gameObject.CompareTag("wall"))
             {
                 print("Hit a Wall!");
             }
 //        if(rayHit.collider.gameObject.tag == "wall")
 //            Instantiate(/*Resources.Load(redWall));
         }
     }
 } 
Comment
Add comment · Show 1
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 whitequill · Jun 17, 2013 at 09:47 PM 0
Share

I solved it. I had my tag wrong. though I am still having trouble with making the object respawn after it has been destroyed.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Jun 17, 2013 at 09:41 PM

you haven't initialised ray

 ray = new GameObject();

might help

Comment
Add comment · Show 1 · 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 bubzy · Jun 17, 2013 at 09:42 PM 0
Share

it might help to use less ambiguous object/variable names too.

avatar image
0

Answer by robertbu · Jun 17, 2013 at 09:43 PM

When posting errors like this one, it is very helpful if you past the error copied directly from the Console window. There are no syntax errors in this code. But you are not initializing 'ray' That is you declare it on like 10, but it is never "hooked up" to any game object. So on line 25, you get an error at runtime. I'm not sure what is going specifically with your Destroy(), but you test for the tag "red" on line 29 but your commented out code destorys "wall". Typically you would want to destroy what your Raycast() hits:

 Destory(hit.collider.gameObject);

As for the prefab and destroy issue, if you really have a true prefab, then I suspect you tried to destroy the prefab and not the scene object.

Comment
Add comment · Show 6 · 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 whitequill · Jun 17, 2013 at 10:14 PM 0
Share

my only problem is I need to re-spawn the object after the tag, "wall", on a separate object, is it by the ray.

So where do I keep an extra version of the object with the tag, "red". that no is invisible?

avatar image whitequill · Jun 17, 2013 at 10:40 PM 0
Share

I've solved half of it:

     //destroy wall
     public GameObject Wall;
 
         if(Physics.Raycast(transform.position, myTransform, out rayHit, 10))
         {
             if(ray.collider.gameObject.CompareTag("red"))
                 Debug.DrawRay(transform.position, Character.transform.forward, Color.red);
                 print("Destroy Wall!");
             Destroy(Wall, 0);

Now I need to find a way to re-spwn "game object Wall", when I hit the tag, "wall". This is difficult because I just destroyed it. and I need an extra copy hiding somewhere.

avatar image bubzy · Jun 17, 2013 at 10:42 PM 0
Share

look up Instantiate

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

avatar image whitequill · Jun 17, 2013 at 11:11 PM 0
Share

that would work if the object I was instantiating wasn't just destroyed...

avatar image bubzy · Jun 18, 2013 at 07:17 AM 0
Share

I would be using prefabs in a situation like this, so you can destroy and instantiate as many times as you like

 //in declarations
 public GameObject wall; //drag the prefab onto this in the editor
 
 //after destruction
 Instantiate(wall,new Vector3(0,0,0),Quaternion.Identity);

this method will allow lots of copies.

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Insatiate and Destroy problem 0 Answers

Raycast problems unexpected token var etc. 2 Answers

Errors, Parsing Errors everywhere :( please help 2 Answers

RaycastHit.textureCoord Always Zero 1 Answer

View ID AllocatedID: X not found during lookup. 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