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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

This was working fine until now?

Discussion in 'Scripting' started by BMRX, May 12, 2015.

  1. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    So I'm getting a few errors here that don't make any sense, everything is pointing in the right direction. I really don't get it.

    Error #1:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. NPCTankController.Initialize () (at Assets/Scripts/AdvancedFSM/NPCTankController.cs:44)
    3. FSM.Start () (at Assets/Scripts/AdvancedFSM/FSM.cs:24)
    NPCTankController.Initialize () line 43 & 44:
    Code (csharp):
    1. //Get the target enemy(Player)
    2.   objPlayer = GameObject.FindGameObjectWithTag("Player");
    3.   playerTransform = objPlayer.transform;
    FSM.Start() line 24:
    Code (csharp):
    1. void Start ()
    2.   {
    3.   Initialize();
    4.    }
    Error #2:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. NPCTankController.FSMFixedUpdate () (at Assets/Scripts/AdvancedFSM/NPCTankController.cs:64)
    3. FSM.FixedUpdate () (at Assets/Scripts/AdvancedFSM/FSM.cs:36)
    NPCTankController.FSMFixedUpdate () line 64
    Code (csharp):
    1. protected override void FSMFixedUpdate()
    2.   {
    3.   CurrentState.Reason(playerTransform, transform);
    4.   CurrentState.Act(playerTransform, transform);
    5.   }
    FSM.FixedUpdate () line 36
    Code (csharp):
    1. void FixedUpdate()
    2.   {
    3.   FSMFixedUpdate();
    4.   }

    This was all working fine yesterday, then I change out the scene and all of my assets for something other than a cylinder and it all goes to heck! Haha

    Any ideas?
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    hazard a guess

    GameObject.FindGameObjectWithTag("Player");

    is not working as you expect
     
    AlucardJay likes this.
  3. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    That would not make a difference.
    It's just weird because this was working as intended before switching out the 3D assets used.

    I have high doubts that changing the asset that this component as attached to would make much difference as it was just a Unity Primitive Capsule. Nothing special about it.
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Its Tag makes all the difference in the world, considering that's what you're looking for.
     
    Last edited: May 12, 2015
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    Exhibit #342 why tags suck and should just be avoided.
     
    Dantus likes this.
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    They suck for finding specific things sure. However, they are useful for figuring out what something is once you've already found it.

    An example - we do line of sight checks from unit to unit. Map props don't break line of sight but buildings do. We do a layer-specific raycast and then check all the things returned by that raycast to see if anything is tagged as a prop.
     
  7. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    BMRX, NullReferenceException is telling you that your code tried to "dot" something that was null. i.e.,
    Code (csharp):
    1. var myThing = null;
    2. myThing.DoSomething();
    3. myThing.Property = 0;
    Both lines 2 and 3 will give a NullReferenceException, so that's why James suggests your FindGameObjectByTag is not working as you expect.

    Consider this line: 34, where the error is happening ...
    Code (csharp):
    1. playerTransform = objPlayer.transform;
    The code is "dotting" objPlayer and throwing a NullReferenceException. This means objPlayer is null.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    lol wouldnt make a difference he says.

    obviously you have no f***** idea, so ill just leave you to it.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,817
    Put a breakpoint on the FindObjectWithTag() call, see what it returns.

    If it is non-null and later becomes null, turn it into a simple property with get/setters, put a breakpoint in the setter, and see who is poking it to null.
     
  10. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Not sure what has crawled up your ass, I was neither rude or insulting to you. I did not question your intelligence or call you out as someone talking out of their ass.

    That said your suggestion is neither constructive nor helpful. You have not presented a "why" that would not be working as intended, as I had previously stated I have everything setup the exact same way it was before. Nothing has changed, as such I have no reason to believe that your "hazard guess" can lead me to any sort of conclusive results.

    If you wish to be helpful to people why not present your suggestions in a more clear way? Your elitist attitude is less than desirable.

    EDIT/

    I thank everyone for their help... It has randomly decided to start working again.

    How did I solve this problem? No idea. G'day
     
    Last edited: May 14, 2015
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you write that you changed nothing and then you write, it worked before you switched out the 3d assets is kind of irritating. It doesn't matter what you changed, but the fact that you changed something matters a lot!
    It seems that you replaced your player in some way and you forgot the "Player" tag. Your code expects this tag, but it is most likely not finding it, which then leads to those errors, because no player could be found and you still try to perform actions with it.
     
    BMRX likes this.
  12. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    I suspect it was something along those lines, yet during this initial problem I had gone over every thing I could think of. Which is what led to this post.

    The odd thing is that I started up Unity tonight and it just started working. Again, I changed nothing. Sorry if that is irritating but that's the best way to describe it.