- Home /
Lives/Death Script
Hi, I've got a game which is two balls on a platform, and they've got to push the other off, best of 3. But I'm struggling with the 'dead' script (when they fall off the side, collide with my floor and lose a life and restart).
The issue is - I'm not using a Character controller on either ball - so CharacterColliderHit doesn't work. How can I rectify that?
Script -
private var dead = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.GameObject.tag == "Death Zone")
{
//dead = true;
Debug.Log ("Dead = True");
//Lives.AmountLives -= 1;
}
}
I want it to find an object (rather than a tag), or make it work on just a general hit, not a controllercollider.
Thanks p.s. I'm really new at this, and I honestly have searched for an answer :(
Answer by fafase · Mar 06, 2012 at 11:33 AM
Use
private var dead = false;
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "Death Zone")
{
dead = true;
Lives.AmountLives -= 1;
}
}
and in update:
if(dead){
transform.position = Vector3(x,y,z); //Location where you want the ball to respawn
dead = false;
}
Now I'm just getting a "NullReferenceException" :( Thanks though. Just not sure what I'm doing wrong though.
You should get more info about this null reference, telling where will help you. Null reference means you are trying to instantiate an object but there is nothing refered to it. LIke you want to shoot a bullet but you have no prefab for the bullet so it is shooting empty.
"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) Lives 2.OnCollisionEnter (UnityEngine.Collision hit) (at Assets/Scripts/Lives 2.js:5)"
Lives 2.js is the above code. This -> if(hit.GameObject.tag == "DeadArea")
I changed the tag of the object it references and the code, numerous times now, aswell to loads of different things. Still doesn't work. Thanks for the response :)
Your answer
Follow this Question
Related Questions
Internal collisions 1 Answer
Find Game Objects With Tag 2 Answers
Yield Continuous Death 1 Answer
how to move the player away when it's hit 3 Answers
Not losing lives 2 Answers