Hello!
I’m working on this schoolproject in my group and we’re trying to make a 2D adventure game.
Everything has been going smoothly until I had to import all my objects (Gamemanager, UI, Player etc) (as prefabs) to a new scene. When I started the game (in editor) for the first time in this new scene many if not all of my object references and other types of references gave me this error: NullReferenceException: Object reference not set to an instance of an object. I know exactly what this means but not why it gave me this error as it was working perfectly before. I’ve checked all “public” references in the inspector to see if any where missing but that wasn’t the case. I’ve even tried to rewrite some of the references but to no result. This all happened when I made the objects my scripts are sitting on into prefabs which I have to. Really appreciate the help as I’m running on a deadline.
Depending on what you did, prefabs can’t reference things in your scene. Example, if I have a player in my scene and an enemy prefab. If the enemy spawns, it will not reference my player right away unless somewhere in code I set that reference up when the enemy spawn.
So if I have a public variable on my enemy and I drag the player into it. Then you turn that enemy into a prefab, when the enemy is instantiated from the prefab, the player variable will not have it’s reference. Hope that helps.
The solution is to hook up the reference when you instantiate. Or, in my opinion, just have a manager class that has a reference to stuff in your scene that can be accessed by newly instantiated objects.
So I looked into the first nullreference error I got when I ran the game and found out what reference didn’t work. However this reference is sitting on one of the prefabs and the reference is referring to a scriptable object that I’ve made which doesn’t make the reference null when I instantiate the prefab. However I’m still getting the nullreference error which is very strange although that in the inspector the scriptable object is in the reference window.
Will probably need to see some code to help you further. Or get a better idea of what you’re doing. It could also be an order of operation issue which makes a value null when you think it’s suppose to have a value.
I just took a guess at the other issue as that tends to be a misunderstanding when prefabs are made.
@unity_dXeMOoww_4rtNg sounds like exactly the bug we experience (Case 1117939) since Unity 2018.3. If you are able please report a bug and upload your project.
Use the project search and t:Script, select all, reimport. Then t:ScriptableObject, select all, reimport, then t: Prefab, select all, reimport. Then see if the references are fixed.
Careful: Deleting Library, Obj or checking out a project from VCS (without those folders if setup correctly) in our case always also leads to the problem and Reimport All also breaks it. I assume the order of reimports is important to fix it - but it is a guess into the blue.
We experience this reproducable in our project everytime we change for example the Application.version, close the editor and open it again. Sadly we had no time to upload a projec to unity yet.
The variable type “Shop” is a scriptable object I’ve made which has all info of the shop. Meaning it has the content list of the shop (“Which items that can be sold at the shop”), values etc. Because It is a scriptable object I need to have it in the inspector as I don’t know any other way to get it into the script. I’ve checked the inspector multiple times and the “ThisShop” variable is not empty.
I tried this but unfortunately it didn’t work. I think I’m going to just redo the whole inventory and shop system I’ve got. I have done another system which I think I can just copy and paste which will hopefully let me continue with the rest that needs to be done. I’m running on a deadline so I don’t have that much time either. Although I couldn’t get a solution for this right now I thank you all for being such quick responders and really trying to help me out.