- Home /
The question is answered, right answer was accepted
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!
Answer by Tomer-Barkan · Nov 05, 2013 at 03:20 PM
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.
I'll accept this as the correct answer, although I have to add that I the "player" from the InputScript was a static value and therefore causing the problem, since it was null.
Answer by ahaykal · Nov 05, 2013 at 01:21 PM
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"); ..
But wouldn`t that be problematic, since the player prefabs are all clones? So if a second player (or more) connects, he would basically take over ALL the player movements? :I And I made it static, because I tested things out, but even if the variable is non static, the player gameobject doesn't get passed onto the InputScript for some reason...
Answer by RyanZimmerman87 · Nov 05, 2013 at 03:12 PM
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.
Also noticed you use 4 entries for your instantiate is that something unique for the PUN network you are using? Assu$$anonymous$$g the 0 is time?
Perhaps the PUN network is causing this somehow but it's most likely something simple, Unity is kind of finicky sometimes with missing object references when setting up new systems.
Answer by drod7425 · Nov 05, 2013 at 03:35 PM
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.