UnassignedReferenceException: Event after assigning? - unity3d

I'm an experienced programmer learning Unity for the first time. I know this is the most basic and common error for the newbies. I think I have got it right but it still produces this error. What am I doing wrong?
UnassignedReferenceException: The variable player of 'GameManager' has not been assigned. You probably need to assign the player variable of the GameManager script in the inspector.
I think the following screenshot provides everything needed.

you have to assign Game Asset in the hierarchy panel to the game object which has GameManager.cs Script Component

I too am new to Unity and had a similar problem, but after thinking about what was going on I came to the conclusion that the error message was misleading me a bit. When it tried to be helpful by suggesting "You probably need to assign the player variable of the GameManager script in the inspector" I went straight to the script and set it but the error persisted.
It was then that I realised that I didn't have an object in the Hierarchy panal which had my script attached. What I was doing was instantiating an object (my 'player' prefab) at runtime and it was the prefab that had the script attached. Of course, my script might be added to any number of objects and each object will have its own variables, so each object needs to have its own instances of its variables set. If you wanted the same value to be set in all instances you probably make your variable static but it won't then appear in the inspector. So, setting the variable in the Script component of my prefab fixed it for me.
I think that's enough waffle for one day - hope it helps somebody! :)

Related

How to access component of a player from instantiated object in PUN 2

I'm trying to learn Photon networking in Unity and ran into a bit of a problem. My player object has a script that instantiates GrenadeObject. Grenade object then has yet another script that deals with OnCollisionDetection and instantiates GroundObject when it touches floor.
Everything's working fine but now I started implementing teams into a game and it turned out to be a little bit tricky. Information about which team certain player is are stored in the GameSettings script that is also attached to a player object. I need to access those information from GroundObject and I'm not sure how to do that. I tried many things but always got NullReferenceException.
Furthest I could get is accessing my GrenadeObject that (as it seems) is owner of GroundObject. I'm assuming something like that is possible since autoCleanUpPlayerObjects removes all GroundObjects intantiated by that player when he leaves. What can I try next?

Unity : Destroy a GameObject that is part of a prefab

The console shows me this error :
Cannot destroy GameObject that is part of a prefab instance. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
I'm confused. The Logs don't tell which prefab or GameObject is involved.
Can u help me ?
Thanks a lot for answers !
Many times when you edit prefabs you will notice that some of them can be blue, while others are with no color. The blue means that they maintain the reference to the original prefab in the project and that they are not a copy, so any change made to them wil be done to the original prefab itself, so the original prebab from which the copies (clones) are made from.
To avoid this, you need to unpack the prefabs, so that you manipulate the copy instead of the original reference.
Probably the error is due to the fact that you are destroying some prefab that is not unpacked, so not destroyable by the runtime GameObject.Destroy that is to destroy a cloned copy from the scene.
Looks like you trying to destroy part of the prefab that is not instanced to the scene. If you need to modify some prefab in editor scripts please refer to PrefabUtility but be aware that this class is accessible only from UnityEditor.
Otherwise please double check your code maybe you using the wrong reference to object after prefab instantiation.
Had this issue recently. A nested prefab was missing in one of the prefabs. Happened when an old prefab was deleted. Check your scene for "Missing Prefab"

Text Mesh Pro UGUI Objects disappearing in Play Mode, Unity Version 2019.3.5fl Personal

So I have been stuck in this issue for awhile now: I get the error message: "Reference Exception: Object reference not set to an instance of an object" but that's because some of my text mesh pro objects gets deleted by itself!!! Apparently every single time I hit play some of the text mesh pro objects disappear from where it was assigned in the first place..
Code Implementation in the script class:
Example: This is how it look before play mode:
Now this is how thei nspector section looks like while on play mode:
Now once I hit play a few text mesh pro objects just got deleted by itself..
Any idea or feedback please?
Thank you in advance!
Amaury
Alright I see what is Wrong.
In the inspector, you set every Text Mesh Pro Object you need.
But in your script, you are using the function GetComponent on every of the object, which is useless and even buggy since you already declarate them in the inspector.
Remove these lines and it should work :)
EDIT: Also, for your interest, the function GetComponent will only return the first component found ATTACHED to the gameObject. But you don't have TMPText attached to your gameObject and more than that, you don't have MULTIPLE TMPText attached.

Object Reference seems not set properly(although it was set properly) in Unity

In my Unity project, although I set the object reference to the instance of the object, I still get the error:
NullReferenceException: Object reference not set to an instance of an
object QuestionCode.CountryChange (UnityEngine.GameObject
button_country) (at Assets/scripts/QuestionCode.cs:898)
However, I am sure that there's no problem with object settings because it was working properly before with exactly same settings I have now.
But I discovered a weird property now and suspect if that might be the problem. As you can see from the figures, my main script for the game has a circular shape(I didn't understand why a script had a geometrical shape anyway) and remains outside of the canvas. This situation doesn't make any sense to me at all.
And unfortunately I can't provide my code here since it's too long and I don't think that there's a problem with the script. So what possible solutions do I have here?
These circular shapes form my QuestionCode script:
Here, you can see Canvas and QuestionCode together. Some parts of questioncode remain outside of the canvas and some cover the whole canvas. And also you can see the error in the console.
The 'geometrical shape' you're seeing in the editor is not from the script. It's a component that's been added to the object somehow. Turn everything off on the gameobject until it goes away, and there's your culprit. That being said, the component being added to your gameobject is not going to cause that error. The error you're getting is definitely from your script, which is most likely because a variable has not been set in the editor. From the pictures you provided, I am unable to see but I'm willing to bet that it's a public variable that has not been set.
I have no idea about what did you do in your code. This error message is so basic that no one can guess the reason. You should check your code following this hit:
An Unity object can NOT work properly with some C# operator like ?.. because an object shows invalid in Inspector windows is still not null in C# code. In this case, it triggers the error message come out.

Unity3D: Instantiating a prefab by its class

In my unity3D project I have a manager script that should take care of initializing a game level, instantiating and placing all relevant prefabs. However, I can't find a way to instantiate a prefab by its class - it turns out that "instantiate" really means something more like "clone" in Unity3D. So as a workaround, I had to make my manager script a MonoBehaviour, attach it to an empty object and assign my prefabs to it from the inspector since it is the only way I have found to make it work at all. This looks kind of awkward. Is there any way to actually instantiate a prefab, without cloning it from a reference?
You can load prefabs at runtime. Just have a look at the script reference which comes with examples:
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
Here is an article that should clarify how it works:
http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html