In my game, the player has the option to throw their ‘sword’ towards others. When they do this it creates a thrown sword prefab and assigns it the ID number of the player who threw it, which it uses to reference a global array containing the color tints for all the players. But for some reason, it isn’t working for me. It spits out the error,
NullReferenceException: Object reference not set to an instance of an object
ThrownSwordController.SetPlayer (Int32 p) (at Assets/Scripts/ThrownSwordController.cs:40)
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:64)
Here’s my code in the thrown sword object
//Set the associated player for this sword
public void SetPlayer(int p) {
associatedPlayer = p;
rend.color = Pallete.playerColors [p];
}
(Where Pallete is a class with some static variables)
And the player object
ThrownSwordController swordCon = swordGO.GetComponent<ThrownSwordController> ();
if (swordCon) {
swordCon.SetPlayer (playerNum);
swordCon.SetMotion (swordPivot.right * throwForce);
}
Any help would be appreciated! Thanks