Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Help with this tutorial

Discussion in 'Scripting' started by d33unity, Nov 27, 2022.

  1. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hello, I try to follow this tutorial:

    In 02:09:40- The DoorCellOpen script:
    When I test the script, The ExtraCross variable and The text "Open the Door" and "E" key (ActionDisplay and ActionKey in the script) appear straight away instead of only appear when the mouse point collide with the door in a distance <2. Then when I am at distance less than 2 at the door, the game stuck and I get the message:"UnassignedReferenceException: the varible ExtraCross of DoorCellOpen has not been assigned". I assigned the variable in the right place in the "DoorTrigger". I don't understand what I am doing wrong here.
    Please help me.

    Here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using StarterAssets;
    6. using TMPro;
    7.  
    8. public class DoorCellOpen : MonoBehaviour
    9. {
    10.     public float TheDistance;
    11.     public GameObject ActionDisplay;
    12.     public GameObject ActionText;
    13.     public GameObject TheDoor;
    14.     public AudioSource CreakSound;
    15.     public GameObject ExtraCross;
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         TheDistance = PlayerCasting.DistanceFromTarget;
    21.     }
    22.  
    23.     void OnMouseOver()
    24.     {
    25.         if (TheDistance <= 2)
    26.         {
    27.          
    28.  
    29.              ExtraCross.SetActive(true);
    30.              ActionDisplay.SetActive(true);
    31.            //ActionDisplay.GetComponent<TMPro.TextMeshProUGUI>().enabled = true;
    32.              ActionText.SetActive(true);
    33.            //ActionText.GetComponent<TMPro.TextMeshProUGUI>().enabled =true;
    34.         }
    35.  
    36.         if (Input.GetButtonDown("Action"))
    37.         {
    38.             if (TheDistance <= 2)
    39.             {
    40.                 this.GetComponent<BoxCollider>().enabled = false;
    41.                ActionDisplay.SetActive(false);
    42.                //ActionDisplay.GetComponent<TMPro.TextMeshProUGUI>().enabled = false;
    43.                ActionText.SetActive(false);
    44.                //ActionText.GetComponent<TMPro.TextMeshProUGUI>().enabled = false;
    45.                 TheDoor.GetComponent<Animation>().Play("FirstDoorOpenAnim");
    46.                 CreakSound.Play();
    47.             }
    48.         }
    49.     }
    50.  
    51.  
    52.     private void OnMouseExit()
    53.     {
    54.         ExtraCross.SetActive(false);
    55.         ActionDisplay.SetActive(false);
    56.         ActionText.SetActive(false);
    57.         //ActionDisplay.GetComponent<TMPro.TextMeshProUGUI>().enabled =false;
    58.        // ActionText.GetComponent<TMPro.TextMeshProUGUI>().enabled = false;
    59.     }
    60.  
    61. }
    62.  
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    Are all field filled in on the CellDoorOpen script when you run the game?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,960
    It's always the same steps to fix this... ALWAYS.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    Be sure that you are doing tutorials properly, or else you are wasting your time!

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  4. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hi! Thank you for your reply to my post, basically when I ran the game I did see all the fields filled on the CellDoorOpen script. Turned out I somehow had a duplicate script attached to a hidden duplicate object. I deleted the duplicate object and the extra copy of the script and I don't get the UnassignedReferenceException error anymore, but the text still appears right away instead of only appearing at distance <=2 at the door. So my debugging work is yet to be completed for now...
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,960
    I don't see you turning it off anywhere except in the mouse callbacks.

    It's customary to assert the active / inactive state you desire during the Start() of your script.
     
  6. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hi, Thank you so much for this detailed reply to my post. As I replied to 'DevDunk', turned out I somehow had a duplicate script attached to a hidden duplicate object. I deleted the duplicate object and the extra copy of the script and I don't get the UnassignedReferenceException error anymore, but the text still appears right away instead of only appearing at distance <=2 at the door. So my debugging work is yet to be completed for now...

    Cute avatar pic BTW. Believe me, I try to follow this tutorial step by step and I would like very much to complete this whole 12 hours tutorial. The problem is that only some time into the tutorial I realized it is a different version of unity then the one the host of the tutorial is using. I also had a major issue with the player object, I basically had to download a starter assets of first and third person controllers which are different from what is shown in the tutorial but still succeeded in completing 2 hours of this tutorial somehow so far.
    I am new to game development but not new to coding as I am familiar with C and C++ also.I understand the intent of the part and the meaning of the script and what it is supposed to do. I guess I really don't have a choice but to go back and redo all of these steps, maybe even download an older version of unity and start all this process over. Oh well, you live and learn...
     
  7. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hi, thank you for your reply. I will try what you suggest in your comment regarding Start() and return here to update. Thanks!
     
  8. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    I watched that part in the video again when he writes the DoorCellOpen script and he just deletes the Start() function (About 01:50:40) so I have no idea how when he starts the game, that text disappears. Anyway, I've decided to just download an older version of unity and start this tutorial all over in order to prevent much more frustration...
     
  9. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    It's probably because he has the two action gameobjects disabled in his hierarchy. Have a look at when he is testing after putting in the script and you will see that they are disabled when he starts and when he gets within range they become active.
    I guess you just missed the bit in the tutorial where he had set them disabled by default.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,960
    I've seen this done in other tutorials, and they rely on it being disabled in scene to start.

    I think this is a terrible approach because it relies in perfect future synchrony between the scene and the program.

    On our team, those two things are frequently edited by different parts of the team.

    Therefore, I always set it to what I want in Start(), and NEVER rely on something being on or off to begin.
     
    Ryiah likes this.
  11. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Yes @ BaBiAGameStudio you are right, I missed that part in 01:35:20 where he explains that he turns off these two text objects and will later write a script to disable/active them. Good to see it works as it should now! Thank you and to everybody else for your help in this thread!