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)
– ricardo_arango1That was my typo while writing the forum post. Sorry :)
– Deadly_FireflyHave you assigned the victim variable in the inspector view?
– ByteSheepThe 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.
– ricardo_arango1The 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
– Deadly_Firefly