How to create tags and layers with code.

I found 1 very old tutorial on google but it assumed you already had a high level of understanding in C#. Does anyone know of any tutorials designed for beginner - intermediate programmers?

  • Jonathan Gonzalez(jgonzalez) replied

    I would instead ask, why do you need to create layers and tags through code? 

  • Hunter & Tiff Eidmann(eithman) replied

    jgonzalez I am working on making the animals in my game capable of forming packs. I thought I would do this by creating a collision box that could detect if any other animals from the same family are within it. If not, they would use a ray cast to seek them out. In the start method I have an int called lineage which is assigned a random number at the beginning of the game and this lineage number is passed to all the children of that animal and all of the subsequent children. I was thinking that I could use code to create tags with this number so that the animals could use that collision box I mentioned before to look for tags with the same lineage number as them. 

    In theory I could manually create a tag for every number within the range of the random number generator but I would have to do this for a thousand numbers. I thought it might be easier to do it in code with a for loop. Am I thinking incorrectly? Is there an easier way to go about this?

  • Jonathan Gonzalez(jgonzalez) replied

    eithman Why not just look for the animals based on the number instead? I assume you're going to have to find their specific tag when they enter a trigger. Instead just search for their number. Whatever enters the trigger zone use GetComponent to get the script that contains their number. Adding a tag won't change anything or make it more effective since you already have a very unique way of identifying them with the number. 


  • Hunter & Tiff Eidmann(eithman) replied

    jgonzalez That's a great idea. I can't believe I didn't think of that in the first place. Thanks!

  • Hunter & Tiff Eidmann(eithman) replied

    jgonzalez   I am not sure how I would type out the code to grab the script of the specific animal that just collided  with me. I know the below is incorrect but could you show me where I am going wrong?


    
    
    private void OnTriggerEnter(Collider col)
    {
    
    if (col.CompareTag("Animal")) // This makes sure it is being triggered by an 
    animal and not a tree or a wall
    
    {
    animal1 = col.gameObject.GetComponent<scriptName>(); // This is my best guess of 
    how to grab the script of the specific animal that just triggered the this animal
    
    if (animal1.Lineage = thisAnimalsLineage)
    {
    // Then it would go along with it's normal functions since a relative is close
    }
    }
    }


  • Jonathan Gonzalez(jgonzalez) replied

    eithman It has a few minor issues and these may be that this is a code snippet and not a full script but I'll point them out anyway. The first is that you haven't declared what animal1 is. You're automatically grabbing the component, but you never specified what type this is initially. Same thing with thisAnimalsLineage, which I assume is a integer. 

    I added both references to these at the top of my script. Lastly, if we wanted to check if these two integer values matches you would use double equals (==). With one equals sign you're assigning value. This is assuming that you're checking the value. If you're assigning it, then you have to include that in the if statement, and not as a condition. This is what it should look like:


    public int thisAnimalsLineage = 5;
    public Animals animal1;
    private void OnTriggerEnter(Collider col)
    {
    if (col.CompareTag("Animal")) // This makes sure it is being triggered by an 
    {
    animal1 = col.gameObject.GetComponent<Animals>(); 
    if (animal1.Lineage == thisAnimalsLineage)
    {
    // Then it would go along with it's normal functions since a relative is close
    Debug.Log("Animal family detected");
    }
    }
    }
    }
    

    Also when I copied your code and pasted it into my script editor the comments wrapped around onto their own line which could cause an error. Make sure if you use "//" you use it for every comment line. If the comment wraps around to the next line it'll be treated as code and could throw an error.