- Home /
Why do I get variable not assigned?
This is my bullet raycast script. Why does this script give me a GameObject victim not assigned? I'm trying to send the GameObject that gets hit by a bullet a message to decrease it's health. The GameObject itself is supposed to be the object that the bullet hits.
public GameObject victim;
void Start()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, Range))
}
transform.parent = hit.transform;
victim = hit.collider.gameObject;
networkView.RPC("TellPlayerToApplyDamage", RPCMode.All);
}
}
[RPC]
void TellPlayerToApplyDamage()
{
victim.gameObject.SendMessage(DamageMethodName, Damage, SendMessageOptions.DontRequireReceiver);
}
UnassignedReferenceException: The variable victim has not been assigned.
Check your curly braces {}. There is a closing brace after if(Physics.Raycast(ray, out hit, Range)
That was my typo while writing the forum post. Sorry :)
Have you assigned the victim variable in the inspector view?
The variable victim could be in another script that is also receiving the TellPlayerToApplyDamage RPC, or the TellPlayerToApplyDamage is being called from another script, not from this Start function.
The Debug.Log("Set victim variable to: "+hit.collider.gameObject.name);
Debug.Log("Set victim variable to: "+victim.name);
after line 13 gives me:
UnnasignedReferenceException: The variable victim has not been assigned.
Set victim variable to: Terrain
Set victim variable to: PistolBullet
When I shoot at a BluePlayerCollider
Answer by Deadly Firefly · Apr 04, 2013 at 04:15 PM
Hey I managed to figure out what I was doing wrong. I should have used
victim = hit.collider;
instead of
victim = hit.collider.gameObject;
Also I changed the SendMessage to SendMessageUpwards.
Thanks to everyone for your help!
Your answer
Follow this Question
Related Questions
Accessing Variables from Another Script 1 Answer
Negative Positions Breaks Raycasts 1 Answer
RaycastHit Collider Question 1 Answer