- Home /
Object reference not set help
Hi!
I am making some kind of multiplayer menu. I need to access a part in a script from another GameObject. It is a variable called "hostData". Of some odd reason, that can't be referenced. I did some testing with "Debug.Log(networkScript.hostData == null);" and it gives me true. That is not good. Everything else can be accessed. Why does this happen?
How I make a reference to the script:
void Start () {
GameObject networkManager = GameObject.FindGameObjectWithTag ("Manager-Network");
networkScript = networkManager.GetComponent<NetworkScript> ();
}
How the variable looks like in the network script:
public HostData[] hostData;
Please tell me what is wrong!
Thanks
Chances are hostData is null. Have you checked this on the NetworkScript?
Check that your GetComponent is working. Test null on networkScript. hostData will also need to be initialised.
I have put this code in the network script and it gives me false.
Debug.Log (hostData == null);
That means it isn't null. But if I do it in the menu script, it gives me null.
Answer by RealMTG · Jul 13, 2014 at 04:31 PM
I found the problem. This was VERY stupid of me. I had to contact my master server and I never did that. I had the function but never contacted the master server. So I added "ContactMasterServer();" when I press multiplayer or whatever to open up the server browser. Simple as that.
Answer by NorthernVisionStudio · Jul 05, 2014 at 03:04 AM
Try using this instead:
using System.Collections.Generic;
//
public List<HostData> hostData = new List<HostData>();
This is much easier and more powerful to manage. And, this will definitely not be null.
You should be able to. Please share the code which you want to access the hostData from so I may better understand. Thanks.
I did a change. I now activate the GUI that needs this in the function but it is missing the reference object. In the Script it is in. How?!
I attached the Script as a txt file so you can take a look at it.
--Removed script--
Your answer
Follow this Question
Related Questions
object not set to instance? 1 Answer
Referencing an object from one script to another, script reference doesn't help :/ 0 Answers
C# Null Object Reference - Driving me mad! 1 Answer
Get variable from seperate script. 1 Answer
Stupid question... Object reference not set to an instance of an object 2 Answers