- Home /
Photon unity issues
Im trying to get this code to work but it just keeps giving me this error for no reason:
NullReferenceException: Object reference not set to an instance of an object
here is my code:
using UnityEngine;
using System.Collections;
public class PlayerSpawn : MonoBehaviour {
public GameObject player;
public GameObject SpawnPoint;
// Use this for initialization
void Start ()
{
MyPlayer ();
}
// Update is called once per frame
void Update ()
{
}
void MyPlayer()
{
GameObject MyPlayerInGame =(GameObject) PhotonNetwork.Instantiate ("soldier3rdPerson 1",SpawnPoint.transform.position,SpawnPoint.transform.rotation,0);
((MonoBehaviour)MyPlayerInGame.GetComponent ("Soldier Movement")).enabled = true;
((MonoBehaviour)MyPlayerInGame.GetComponent ("Crouch Controller")).enabled = true;
((MonoBehaviour)MyPlayerInGame.GetComponent ("Skeleton To Character")).enabled = true;
((MonoBehaviour)MyPlayerInGame.GetComponent ("Soldier LOD")).enabled = true;
((MonoBehaviour)MyPlayerInGame.GetComponent ("Health")).enabled = true;
((MonoBehaviour)MyPlayerInGame.GetComponent ("Trigger Children Collider")).enabled = true;
}
}
Answer by kbaloch · Feb 19, 2015 at 11:40 AM
Drag your Player game object to your player variable in this script in inspector window. this error will disappear . or find this using tag or name in start
start() {
player = GameObject.FindGameobjectWithTag("your tag");
}
Answer by Abysinian · Feb 19, 2015 at 01:54 PM
This is because you're referencing SpawnPoint in your instantiate statement, but haven't set SpawnPoint to anything. This means the Instantiate is trying to put your player at the position of an undefined GameObject (hence the error of an object reference not being set to an instance of an object).
To solve this, you'll need to do something along the lines of:
SpawnPoint = GameObject.FindGameObjectWithTag("SpawnPoint");
thats not the problem, i have added them in the inspector, its with line 26 ???
If it's with Line 26 then that might suggest there is no component with that name attached to the player. You're not really providing any useful information, you're just asking what it is.
sorry, the components im getting are attached to the player and the error i keep getting is:
NullReferenceException: Object reference not set to an instance of an object