- Home /
What am I doing wrong with this collision code?
public class BulletScript : MonoBehaviour {
public GameObject hitEffect;
private Transform myTransform;
public float bulletSpeed = 0.1f;
private bool spent = false;
private RaycastHit hit;
public float rayRange = 1.5f;
// Use this for initialization
void OnEnable () {
myTransform = transform;
}
// Update is called once per frame
void Update () {
myTransform.Translate(Vector3.up * bulletSpeed * Time.deltaTime);
if(Physics.Raycast(myTransform.position, myTransform.up, out hit, rayRange)&&
spent == false){
if(hit.transform.tag == "Floor"){
GameObject newHitEffect = (GameObject)Instantiate(hitEffect, hit.point, Quaternion.identity);
Destroy(newHitEffect, 2);
spent = true;
GetComponent<PoolObject>().ReturnToPool();
}
spent = false;
}else if(hit.transform.tag == "Target"){
Destroy(this.gameObject);
}
}
}
Getting an error for the hit on the Target tag (line 46)
"Object reference not set to an instance of an object"
The projectile passes right through the cube tagged Target and I get that error. What did I do wrong here?
I also face this problem(Object reference not set to an instance of an object) but in my case i was destroying the object on which script is added, then i create reference of that object and i deleted reference object.
Answer by Visal · Jun 30, 2014 at 07:00 AM
The reason because hit is null. You tried to check hit.transform.tag
while the raycast might not hit any object. I think you putted the else if in the wrong block. It should be after if(hit.transform.tag == "Floor")
block which is:
if(hit.transform.tag == "Floor"){
GameObject newHitEffect = (GameObject)Instantiate(hitEffect, hit.point, Quaternion.identity);
Destroy(newHitEffect, 2);
spent = true;
GetComponent<PoolObject>().ReturnToPool();
}
else if(hit.transform.tag == "Target"){
Destroy(this.gameObject);
}
Hope it'll help.
Thanks, this solved the error but the object tagged Target still isn't destroyed. I added a Debug.Log message on hit, which pops up when the projectile hits the target, but the target remains.
Is Destroy(this.gameObject) not the proper way to destroy the object the projectile hits?
Also added an AddForceAtPosition for a rigibody hit, which works. So I'm assu$$anonymous$$g Destroy(this.gameObject) is incorrect in this context. But you solved my error, so thank you! And thank you to all the other answers, too.
Answer by dumbat · Jun 30, 2014 at 06:52 AM
In your conditional at line 29, it is evaluating spent==false first, and not getting to the Physics.Raycast call. Your hit variable is uninitialised
Answer by Huacanacha · Jun 30, 2014 at 06:53 AM
The ELSE IF can execute if there is no collision, in which case "hit" is not populated. Just check the collision in the first IF, then use an "if (spent == false)" / "else if (hit.transform.tag == "Target")" branch within the first IF.
if(Physics.Raycast(myTransform.position, myTransform.up, out hit, rayRange)) {
if (spent == false){
if(hit.transform.tag == "Floor"){
GameObject newHitEffect = (GameObject)Instantiate(hitEffect, hit.point, Quaternion.identity);
Destroy(newHitEffect, 2);
spent = true;
GetComponent<PoolObject>().ReturnToPool();
}
spent = false;
} else if(hit.transform.tag == "Target"){
Destroy(this.gameObject);
}
}
Answer by HarshadK · Jun 30, 2014 at 06:58 AM
The problem is with your placement of if...else loop.
Your else loop is for your if(Physics.Raycast...)
loop which makes your hit object unavailable in your else loop since your hit object is for Raycast loop only.
I think your want to put your else loop for if(hit.transform.tag == "Floor"){
line of code.