Setting Player Instance for Scripts doesn't work.

Hello there, I am currently sitting on a really crappy problem, below you see my code where the player gets spawned into the world (using the PUN Plugin) and that works fine, the camera gets attached as well and follows the player accordingly, but I just don’t get the InputScript to work.

void OnJoinedRoom()
	{
		player = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);	//Instantiate the Player Clone
		Camera.main.GetComponent<CameraScript>().target = player.transform;					//Let the Camera Target the player
		InputScript.fetch_movement = true;	// Start fetching input to move character
		InputScript.target = player;		// Give InputScript the player gameobject
	}

It gives ingame the error that: “Object Reference is no set to an Instance of an Object”… and I tried various things but I can’t get it to work how I want it…

Here’s my InputScript:
public class InputScript : MonoBehaviour
{
public static bool fetch_movement;

	public static GameObject target;
	
	void Update ()
	{
		if(fetch_movement)
		{
			
			if(Input.GetKeyDown(KeyCode.A))
			{
				target.GetComponent<PlayerMovement>().moving_left = true;
			}
			if(Input.GetKeyUp (KeyCode.A))
			{
				target.GetComponent<PlayerMovement>().moving_left = false;
			}
			if(Input.GetKeyDown(KeyCode.D))
			{
				target.GetComponent<PlayerMovement>().moving_right = true; 
			}
			if(Input.GetKeyUp (KeyCode.D))
			{
				target.GetComponent<PlayerMovement>().moving_right = false;
			}
			if(Input.GetKeyDown(KeyCode.W))
			{
				target.GetComponent<PlayerMovement>().moving_up = true;
			}
			if(Input.GetKeyUp (KeyCode.W))
			{
				target.GetComponent<PlayerMovement>().moving_up = false;
			}
			if(Input.GetKeyDown(KeyCode.S))
			{
				target.GetComponent<PlayerMovement>().moving_down = true;
			}
			if(Input.GetKeyUp (KeyCode.S))
			{
				target.GetComponent<PlayerMovement>().moving_down = false;
			}
		}
	}
}

Hint: The game is 2D, in case you were wondering why W is used for UP and S for DOWN.

I appreciate any kind of help/ideas!

the problem is with this:

public static GameObject target;
you need to initialize this value… why do you have it static?
target = GameObject.Find(“whatever_It_is_Called”);

Kinda hard to answer without knowing how the PhotonNetwork functions but perhaps that isn’t really relevant.

Normally if I instantiate new objects I use a clone. For example:

//prefab
public GameObject playerPrefab;
//instantiate object
GameObject newPlayerObject;

newPlayerObject = Instantiate(playerPrefab, , Vector3.zero, Quaternion.identity, 0);

That might help you a little bit since you mentioned concern about duplicate objects, you should never need a static variable for this kind of thing unless I’m missing something with your project.

However with your initial error message that might be caused by not having as GameObject at the end of your instantiate? I would think you wouldn’t be able to compile if that were the case but you might wanna check that out. For example:

newPlayerObject = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity, 0) as GameObject;

Maybe the as Gameobject is only necessary if you use the clone method not sure exactly.

Your target is probably null before the player is spawned. In Update(), add:

if (target == null) 
    return;

In general it is good practice to always check that an object is not null, specially when you know it is at some point.

If this doesn’t help, please share the exact line which throws the null pointer exception.

This seems unnecessary. Why not just attach the InputScript to the character prefab? Set the prefab as the target in the inspector. Then once the player is instantiated, there’s no turning things on, it just starts working.