- Home /
Question by
sandrush73 · Jul 14, 2014 at 01:27 AM ·
raycastraycasthitgameobject.tag
Null reference error can u help me to fix it ....!
hello all i am bit new to scripting in unity. im having a small issue, i want to specific effect when i hit a specific gameobject with a tag. from my code i want to shift from 1st player to second when i hit a game object with a tag 'switch' but when i do it im getting null reference expected error please help me in fixing it thanks in advance ...!
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast (ray, hit, 100))
{
switchPlayer1 = ! switchPlayer1;
if (hit.collider.gameObject.tag == "switch")
{
switchPlayer1 = true;
}
}
}
Comment
Please format your code properly so that all your code is formatted and not just the ending brackets. This time it is done for you but be careful from next time onward.
Also on which line are you getting this null reference error?
here is my code i want to switch from player 1 to player 2 when i click on the gameobject with tag, but when i use stuff like print function it works fine
var hit : RaycastHit;
var player1 : GameObject;
var player2 GameObject;
var switchPlayer : boolean = false;
var playerActive : int = 0;
var particlePoof : GameObject;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
function Start ()
{
{
switch(playerActive) {
case 0:
playerActive = 1;
Debug.Log(playerActive);
var poof3 = Instantiate (particlePoof, player2.transform.position, Quaternion.identity);
Destroy (poof3, 3.5);
break;
case 1:
playerActive = 0;
Debug.Log(playerActive);
var poof4 = Instantiate (particlePoof, player1.transform.position, Quaternion.identity);
Destroy (poof4, 3.5);
break;
}
}
if (Input.Get$$anonymous$$ouseButtonDown (0))
{
if (Physics.Raycast (ray, hit, 100))
{
if (hit.collider.gameObject.tag == "switch")
{
switchPlayer = true;
}
else
{
switchPlayer = false;
}
}
}
}
function Update ()
if(switchPlayer == true){
switch(playerActive) {
case 0:
playerActive = 0;
Debug.Log(playerActive);
break;
case 1:
playerActive = 1;
Debug.Log(playerActive);
break;
}
}