I’m trying to get a component (“Stats”) written in C# from a script written in Java:
var tempStats = GameObject.Find(“Player”).GetComponent(“Stats”);
Debug.Log(tempStats.health);
//or other things involving this data…
I get a NullReferenceException whenever I try to do anything with the stats. The thing is, this code seemed to be working just fine yesterday, and the “Stats” component is inside the “Player” object and seems to be running fine on its own (I’ve checked using Debug.Logs and the like). I’m doing this in a few different places, and all were working, and now all are breaking with the same NullReference error.
I believe the syntax is correct (please let me know if it isn’t), and I suspect the issue might be with the source control. We’re using Tortoise, and the darn thing keeps losing track of where the attached scripts are within the game components. I’ve run through the documentation “Creating a new project and importing it to a Subversion repository” and made sure I only added the library files I needed.
I’ve removed and re-added the “Stats” component to the player object, and the script still cannot find it. I don’t know if I need to rebuild the scripts or re-import the assets (I’ve tried both), but from a distance it appears the script stopped working for no reason, and looking a little closer it appears it stopped working due to something the version control software did. While I’d normally chalk it up to coder error there are several places where the code breaks where it did not a day ago.
If there’s a simple error in code, I’d appreciate any input (or other methods to access C# variables form Java and vice versa). Otherwise, having the version control forget where component scripts and other assets are is quickly becoming untenable, and any input or advice on this front would be equally appreciated!
That is good to know - although I don’t think that’s the problem. I tried moving the “Stats” script to the “Standard Assets/Scripts” folder so it compiles earlier, but I still get the same error - and the whole setup used to work fine before, without this consideration.
Would the source control have messed the whole thing up due to this form of compiling-order-by-folder? I’m going to try a major “re-import assets”, but I doubt it will do much, and even if it does, I still want to know what went wrong.
You could try using the class name (I think the file has to be called Stats.cs, but I forget)
var tempStats : Stats;
tempStats = GameObject.Find("Player").GetComponent(Stats);
Debug.Log(tempStats.health);
I keep getting “NullReferenceException: Object reference not set to an instance of an object” - the class “Stats” is a .cs format, and I have been able to access its data from a java script before. I still think the problem is that the Gameobject “Player” doesn’t think it has the component “Stats” connected to it, even through I have it added in the inspection window. The above code either gives a NullReference or won’t compile with a “health is not a member of ‘Stats’” sort of error (it is), which makes me think it’s not finding the object, and assumes the class name “Stats” is something else. Still scratching my head over this one…
OK - Kinda silly, but it works now - Unity had found an older script with the same name, and got confused, loading that file (a java one) instead of the newer C# one. This even overrode the fact that I had added the C# script to directly to the player object. The confusion must have started during a version control update where it switched the two, but for the most part it was a silly mistake on my part. Make sure you don’t have files with duplicate names!
Thanks again for the prompt responses and help!