- Home /
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));
}
}
}
I solved it. I had my tag wrong. though I am still having trouble with making the object respawn after it has been destroyed.
Answer by bubzy · Jun 17, 2013 at 09:41 PM
you haven't initialised ray
ray = new GameObject();
might help
it might help to use less ambiguous object/variable names too.
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.
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?
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.
that would work if the object I was instantiating wasn't just destroyed...
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.
Your answer
Follow this Question
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