Search Unity

Object reference shows in play mode but console gives null error

Discussion in 'Scripting' started by luke1513, Aug 9, 2019.

  1. luke1513

    luke1513

    Joined:
    Sep 19, 2018
    Posts:
    5
    Hello. It's kind of a complicated problem, i don't know if it's my code's fault or unity's, i actually give up and ask the first time on a forum for unity help. First, here is the problem:

    https://imgur.com/a/6Hu58Ik

    And here is the code needed for understanding:

    Code (CSharp):
    1. IEnumerator StartQueue(Interactable currentInteractable)
    2.     {
    3.         xx = false;
    4.         nextInQueue = null;
    5.         agent.SetDestination(currentInteractable.interactPos);
    6.         yield return new WaitForSeconds(Time.deltaTime);
    7.         StartCoroutine(GetNextInQueue(currentInteractable, currentInteractable.lastInQueue));
    8.         while (xx == false && agent.remainingDistance > agent.stoppingDistance)
    9.             yield return new WaitForSeconds(Time.deltaTime);
    10.         StopCoroutine("GetNextInQueue");
    11.         nextInQueue = currentInteractable.lastInQueue;
    12.         currentInteractable.NextInQueue(this);
    13.         while (nextInQueue && !nextInQueue.finished)
    14.         {
    15.             agent.SetDestination(nextInQueue.transform.GetChild(0).position);
    16.             yield return new WaitForSeconds(Time.deltaTime);
    17.         }
    18.         agent.SetDestination(currentInteractable.interactPos);
    19.         yield return new WaitForSeconds(Time.deltaTime);
    20.         while (agent.remainingDistance > agent.stoppingDistance)
    21.             yield return new WaitForSeconds(Time.deltaTime);
    22.         arrived = true;
    23.     }
    Code (CSharp):
    1. IEnumerator GetNextInQueue(Interactable currentInteractable, Customer lastInQueue)
    2.     {
    3.         while (lastInQueue == currentInteractable.lastInQueue)
    4.             yield return new WaitForSeconds(Time.deltaTime);
    5.         xx = true;
    6.     }
    Code (CSharp):
    1. public class Interactable : MonoBehaviour
    2. {
    3.     public Vector3 interactPos;
    4.     public Customer lastInQueue = null;
    5.  
    6.     public void NextInQueue(Customer currentInQueue)
    7.     {
    8.         lastInQueue = currentInQueue;
    9.     }
    10.  
    11.     public void FinishQueue(Customer currentInQueue)
    12.     {
    13.         Debug.Log(currentInQueue.name); // here is what is shown in the console
    14.         Debug.Log(lastInQueue.name);    // (only the last 2 out of the total 4 lines matter, in the first 2 lines the customer goes to buy something, also it works very well there, the shelves inherit from the Interactable class too, and i use the StartQueue coroutine without problem, only on the register this weird thing happens)
    15.         if (currentInQueue == lastInQueue)
    16.         {
    17.          
    18.             lastInQueue = null;
    19.         }
    20.         currentInQueue.finished = false;
    21.     }
    22. }

    Code (CSharp):
    1. public class Register : Interactable
    2. {
    3.     void Start()
    4.     {
    5.         interactPos = transform.GetChild(0).position;
    6.     }
    7. }
    Basically, when FinishQueue is called, the if statement should be executed because lastInQueue is equal to currentInQueue, according to logic and the inspector, but somehow lastInQueue it's null. I updated unity, the script register is not attached in multiple places, i don't have multiple inspectors open. I don't know what to do
     
  2. Probably yours.

    Please, include the screenshots on the forums. We don't like to visit external resources when we try to decipher your problems. You can upload images by hitting the "Upload a File" when you edit your post. You can insert the screenshot in your post directly.

    Now, about your problem.
    Which line here is the Buyable.cs 19th line? Because your error says the problem is there, but I cannot guess which code piece belongs to the Buyable.cs and which one is the 19th line.
    Also, have you checked what property or variable is null on that line?
     
  3. luke1513

    luke1513

    Joined:
    Sep 19, 2018
    Posts:
    5
    Sorry, i tried to upload it but it didn't work. The line 19 is from the Interactable class, "Debug.Log(lastInQueue.name);". The editor says there is a reference present, i highlighted it. Also, in the register class, in the Update() function i tried the same line, "Debug.Log(lastInQueue.name);", and it shows null until the customer reaches the register, basically it is equivalent to the editor, showing null when its null, and as soon as the reference field is 'full', with "Customer (Customer)", the Debug.Log() outputs "Customer". Its basically a contradiction between multiple things, which should usually "be on the same side", very weird.
     
    Last edited: Aug 9, 2019
  4. luke1513

    luke1513

    Joined:
    Sep 19, 2018
    Posts:
    5
    I cannot continue my project because of this nonsense problem and nobody seems to know whats happening, i really don't know what to do. There seems to be another cause, not my code's fault, since i tested it many times on the shelves.
     
  5. mgnz

    mgnz

    Joined:
    Apr 2, 2016
    Posts:
    6
    Have you tried rebuilding library folder?