- Home /
NullReferenceException help
Why do I get:
SyncPlayerPosRot.Update () (at Assets/_Scripts/SyncPlayerPosRot.cs:27) Why are the variables in the GameController NULL ? And what does that mean? And how do I fix it ?NullReferenceException: Object reference not set to an instance of an object
using UnityEngine;
using System.Collections;
using TNet;
public class SyncPlayerPosRot : TNBehaviour
{
private GameController gC;
float mNextUpdate = 0f;
// Use this for initialization
void Start ()
{
gC = GetComponent<GameController>();
}
// Update is called once per frame
void Update ()
{
if (mNextUpdate < Time.time)
{
mNextUpdate = Time.time + 0.01f;
if (tno.isMine)
{
gC.Player.Position = transform.position;
gC.Player.Rotation = transform.rotation;
tno.SendQuickly("SetPos", TNet.Target.OthersSaved, transform.position);
tno.SendQuickly("SetRot", TNet.Target.OthersSaved, transform.rotation);
}
}
}
[RFC] void SetPos (Vector3 pos) { transform.position = pos; }
[RFC] void SetRot (Quaternion rot) { transform.rotation = rot; }
}
Here is the GameController script->
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public class PlayerSettings
{
public string Name;
public int ID = 0;
public string Team;
public bool Spawned = false;
public Vector3 Position;
public Quaternion Rotation;
}
public PlayerSettings Player = new PlayerSettings();
}
What is line 29 in the code? And what is the tno object?
tno is TNet stuff, and the line 29 it should say 27, (was me who edited the code a bit before I pasted the error message) but anyway, it is complaining about line 27, and that is this one:
gC.Player.Position = transform.position;
Also You got the second object with its class below in code...
In start add a check for your reference:
if(gC == null) print("Not found");
Then it means your script is not attached to the same object.
Thanks, that was what I was looking for, I had already made a Null check, but didnt know what it mean, As I dont really understand Null :/
How would I do if I dont want to reference to the object by adding it like "public transform myObj" and drag it to that slot in editor?
Thanks!
null
means "nothing", non existant. If you want more detail, google it.
As for your second question... google it. Please do not post questions in comments :)
Answer by ThisIsMarkS · Jul 16, 2013 at 04:04 PM
I tried running your code on my pc and no error was found
Maybe your "gameController" Script is not assigned to an object.
That's why it is giving a NullReference Exception
Please Check!