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

Object reference not set to an instance of an object in my code and I don t know why

Discussion in 'Scripting' started by festy22, Jun 9, 2022.

  1. festy22

    festy22

    Joined:
    Jun 6, 2022
    Posts:
    3
    I have been getting this error while coping a course and i dont know why.
    This is my code :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using RPG.Move;
    4. using RPG.Core;
    5.  
    6.  
    7. namespace RPG.Lupta
    8. {
    9.  
    10.  
    11.     public class Luptator : MonoBehaviour, IActiune
    12.     {
    13.  
    14.         [SerializeField] float distantaArma = 2f;
    15.         [SerializeField] float timpIntreAtacuri = 1f;
    16.         [SerializeField] float armaDauna = 5f;
    17.         Sanatate tinta;
    18.         float timpUltimulAtac = 0;
    19.         public void Update()
    20.         {
    21.             timpUltimulAtac += Time.deltaTime;
    22.  
    23.             if (tinta == null) return;
    24.             if (tinta.EsteMort()) return;
    25.  
    26.             if (!GetIsInRange())
    27.             {
    28.              
    29.                 GetComponent<Mover>().MutareCatre(tinta.transform.position);
    30.  
    31.             }
    32.             else
    33.             {
    34.                 GetComponent<Mover>().Cancel();
    35.                 ComportamentLovitura();
    36.  
    37.             }
    38.  
    39.         }
    40.  
    41.         private void ComportamentLovitura()
    42.         {
    43.             transform.LookAt(tinta.transform);
    44.             if (timpUltimulAtac > timpIntreAtacuri)
    45.             {
    46.                 //declanseaza evenimentul Hit()
    47.                 TriggerAtac();
    48.                 timpUltimulAtac = 0;
    49.  
    50.             }
    51.  
    52.         }
    53.  
    54.         private void TriggerAtac()
    55.         {
    56.             GetComponent<Animator>().ResetTrigger("stopAtac");
    57.             GetComponent<Animator>().SetTrigger("atac");
    58.         }
    59.  
    60.         //este un event animatie
    61.         void Hit()
    62.         {
    63.             if (tinta == null ) { return; }
    64.             tinta.iaDaunele(armaDauna);
    65.          
    66.         }
    67.  
    68.    
    69.         private bool GetIsInRange()
    70.         {
    71.             return Vector3.Distance(transform.position, tinta.transform.position) < distantaArma;
    72.         }
    73.  
    74.         public bool PoateAtaca(GameObject tintaLupta)
    75.         {
    76.             if (tintaLupta == null) { return false; }
    77.             Sanatate tintaDeTest = tintaLupta.GetComponent<Sanatate>();
    78.             return tintaDeTest != null && !tintaDeTest.EsteMort();
    79.         }
    80.         public void Atac(GameObject tintaLupta)
    81.         {
    82.             GetComponent<ProgramareActiune>().IncepeActiune(this);
    83.             tinta = tintaLupta.GetComponent<Sanatate>();
    84.         }
    85.  
    86.         public void Cancel()
    87.         {
    88.             StopAtac();
    89.             tinta = null;
    90.         }
    91.  
    92.         private void StopAtac()
    93.         {
    94.             GetComponent<Animator>().ResetTrigger("atac");
    95.             GetComponent<Animator>().SetTrigger("stopAtac");
    96.         }
    97.  
    98.  
    99.     }
    100.  
    101.  
    102. }
    103.  
    104.  
    105.  
    106.  

    It says that the problem is in line 28 at the first code i put. which is :
    GetComponent<Mover>().MutareCatre(tinta.transform.position);

    Does anyone know what the problem might be?[/CODE]
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
  3. festy22

    festy22

    Joined:
    Jun 6, 2022
    Posts:
    3
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    You never need to post for a nullreference. Why? Because it doesn't matter.

    There is only ONE way to fix it, and it has THREE steps.

    In fact it's such a common error, we have a pinned post!

    How to fix a NullReferenceException error

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

    Steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that
     
    LiterallyJeff likes this.
  5. festy22

    festy22

    Joined:
    Jun 6, 2022
    Posts:
    3
    I tried everything, it doesn't work. And the odd part is that it was working perfectly until I changed something in another script, but I don t know what the probloem is
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    I didn't tell you to try everything. That is NEVER going to be useful.

    Step #1 is identify what is null.

    Let us know when you get Step #1 out of the way.

    We will know you have done Step #1 when you come here and post, "the variable named BLAH is what is null and it happens on line XXX."

    ALSO:

    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!