Home Unity

Unity Excption: Tag: null is not Defined (C#)

Hello guys, thanks for taking the time to help me out.

I'm running into a bit of a problem in my game I'm working on.

The script is working correctly at runtime; and when I encounter enemies, the combat gets loaded and set up correctly.

However, when I run the combat 'for each' loop that iterates through each member in a combat list, I get this error:

o79j.jpg

I think I traced it to this particular block of code circled in the image below. I think the exception might be thrown because the script is trying to access the variable between value changes...

While I have learned a lot about C# in the past few weeks, I am still relatively new to coding. After doing some research, I am unable to find what the issue is.

rxyv.jpg


I've attached the entire script if you have the time to look at it.
http://pastebin.com/UnvifGZC

Replies

  • electricsauce
    Offline / Send Message
    electricsauce polycounter lvl 11
    I don't see the list being instantiated anywhere:

    public List<string> combatList; = new List<string>();

    also, you should get in the habit of not using var.
  • MikeSeguin
    Thanks electricsauce.

    I should note that before I added the bit to make characters turn and rotate to face active defenders/attackers, the for loop was iterating successfully.
  • electricsauce
    Offline / Send Message
    electricsauce polycounter lvl 11
    I don't see anything obvious that would cause the combatList to be null. It looks like it shouldn't be able to get into that loop to throw that error on var targetLookPoint = GameObject.FindWithTag(value);

    Can you put in a debug.log(value); after the foreach loop?
  • rollin
    Offline / Send Message
    rollin polycounter
    you simply have something in the list which is there but null.. maybe because it was destroyed or something.. best way imo is to simply check inside the foreach that your next object in combatList is what you expect.. and that is not null
  • Farfarer
    What is the activedefender variable? It seems to me like if be a class or struct.

    And converting that to string might not be possible unless you've given it that ability.

    I suspect you want activedefender.name or something. So that it's a string.
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    I think Farfarer got it.

    But specifically, the code block that you have highlighted references ActiveDefender.ToString() but ActiveDefender has not been initialized before that code executes, so it must be a null value. Nor has Active Combatant for that matter.

    You might just break the lookat bit into its own little function:
    private void FaceOff(GameObject attacker, GameObject defender)
    	{
    		attacker.transform.rotation = Quaternion.LookRotation(defender.transform.position, attacker.transform.up);
    		defender.transform.rotation = Quaternion.LookRotation(attacker.transform.position, defender.transform.up);
    	
    	}
    
    then modify your combat loop to be something like
    	// if the current value is Enemy01, he does his action.
    			if (value == "Enemy01"){
    				Debug.Log (value + " attacked you.");
    				// Sets up dynamic variables for action sequence
    				activeCombatant = GameObject.FindWithTag (value);
    				activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
    				activeDefender = GameObject.FindWithTag("Player");
    				activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
    ///////////////////face off here before attack runs///////////////////
    				FaceOff(activeCombatant,activeDefender);
    /////////////////////////////////////////////////////
    				// Performs Test attack sequence against the player
    				attackSequence();
    			}
    			
    
  • MikeSeguin
    Thanks for the help guys.

    Turns out two variables weren't getting values before it hit the for each loop.

    Much appreciated!
Sign In or Register to comment.