Advertisement

I need help with unity... :(

Started by July 30, 2018 05:10 PM
11 comments, last by Enitalp 6 years, 6 months ago

Hello, i know this is the most random thing, but i need help, i am trying to make an idle game myself and publish it, but i am learning code by myself, and i am really lost, i got stuck with a null reference exception error when i was refering to variables and functions from another script and i have been stuck for hours the references work in start and update methods but not in the method i am trying to make for my buy button, i tried alot and alot, i think i spent over 7 hours trying to figure it out and whenever i asked anyone on any forrum who could help they refused and one said and i quote '' this is the simplest error you may as well leave game development'' and i really dont want to i am working against my circumstances and studying against what my parents want me to study( i am 17) i thought maybe you could relate and help, if you can help me please message me and ill send the code so you can maybe take a look at it, thank you.

hi,

i can help you, no problem, if i can see the code and the crash i will give you what you "did wrong"

 

Advertisement

thank you very much, you are very kind , here are the scripts , and when i click on the buy button that calls the buystore() function i get this message:

NullReferenceException: Object reference not set to an instance of an object
Store.buyStore () (at Assets/Scripts/Store.cs:58)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
 

GameManager.cs

Store.cs

Ok, that's an "easy" one.

In store you have a GameManager gm; and gm is not initialized.  

you should in the inspector point to the gameobject containining your GameManager.

 

 

 

 

 

i did, i dragged the game object in the inspector.

 

maybe there is something else, i just checked if i did again and yes i did.

 

the weird thing is that i can use the variables in the start and update methods but not down in the buy store method, maybe this information can help. an example of that is i did the gm.addToBalance in the update method and it worked fine.

So you should have something like that ?

 

Works for me as there is no reason for it to not work unless you point to the wrong store behavior in your button, and this store doesn't have a gm

 

Store2.JPG

Store1.JPG

Advertisement

oh, maybe its because i have 2 stores, my store is a prefab, does that have anything to do with it, because the parts you highlighted i have them the same except the object i have has a different name.

nvm i love you.

 

it worked, it worked!!!!!!!!!!!!!!!!!!!! it worked after all that time.

thank you, i dont know, the buttons were linked and they did have the store and the function as buy store, but i tried linking them again and through some magic it worked, THANK YOU VERY MUCH!!!

So your GameManager is also in the store prefab ? because a prefab can't have a reference to an object in the scene. If you edited the prefab in your scene and made a link to your GameManager, then apply the prefab, and then later spawn the prefab, it doesn't work. Only work if you specialized the prefab that stay in your scene.

 

4 minutes ago, Enitalp said:

e prefab in your scene and made a link to your GameManager, then apply the prefab, and then later spawn the

no i dont think so, i dont know right now i jumped out of my seat, again thank you i ran, ive been trying for more than 7 hours and i am pretty sure i reset the buttons many times, i dont know why it worked this time, but i am not complaining, thank god it worked. and no i dont think the gamemanager was linked in the prefab, i am pretty sure i did that after, but the store itself with its variables and functions is a prefab, anyways i am gonna work on it now, you really dont know how much i appreciate you helping me, i worked on this game for over 13 hours yesterday, even though i dont know much i really do love coding so thank you very much.

One architecture to get rid of the problem once for all.

Put your gamemanager on a gameobject in your scene.

Remove the reference on the gamemamanger in your store script.

change your gamemanger code for :


   static public GameManager Instance;
    // Use this for initialization
    void Start()
    {
        currentBalance.text = "$" + balance.ToString();

        Instance = this;
    }

 

and in store.cs


 


 public void buyStore()
    {
        if (GameManage.Instance.canBuy(baseStorePrice))
        {
            GameManage.Instance.addToBalance(-baseStorePrice

 

This topic is closed to new replies.

Advertisement