Search Unity

How to make a script look for a Tag Name then a Variable?

Discussion in 'Scripting' started by MonkeyZero, Feb 1, 2015.

  1. MonkeyZero

    MonkeyZero

    Joined:
    Feb 20, 2013
    Posts:
    37
    Okay, so here is what I have created thus far.
    1. I created a prefab object that is visible in the editor only via a layer.
    2. It has also been given a tag of Marker.
    3. In this game object is a simple reference box of the player size with another pointing in the +Z axis so that I can know what direction it is facing.
    4. On the parent Game Object I have a Java with one variable "markerAddress" which is a string.
    I am trying to figure out how to make another script that takes a string input from the Level Switching Trigger I've created, and once the scene is loaded seeks out any object with the Marker tag and then inspects each looking for the Matching "markerAddress" string that was supplied from the Scene before when the Trigger was activated. Once the matching SpawnMarker is found, the Spawner Script then Instantiates the Player Controller Prefab right where the SpawnMarker is and rotated to match it's facing direction.

    I have the Spawner Already aware of what the "destinationAddress" should be, and it already looks for GameObject with the tag of "Marker" and see that just fine. It creates the Player Controller, What ever one i have in the GameObject public variable of the script. But am getting hung up on how to get this script to drill deeper into the multiple marker objects in the scene to pick out the one with the matching "markerAddress" string.

    Also do I store a list of all the Markers to run down the listed object looking for the matching combo?

    I don't have the SceneSwitching or Spawner Scripts on this computer. Both are written in C# rather then Java. I hope that C# can drill into a Java Script to analyze the string and compare it for a match.

    Any ideas at this point of how I could grab the string value of a tagged game object would be most helpful. If it works I'll publish the scripts and prefabs on the asset store for free so no one ever has to struggle with this again.
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    This is how to do some of these things in C#.
    Code (CSharp):
    1. // Find marker gameobjects
    2. GameObject[] markers;
    3.  
    4. markers = GameObject.FindGameObjectsWithTag("Marker");
    Code (CSharp):
    1. // Look through marker gameobjects
    2. foreach (GameObject go in markers)
    3. {
    4.     // Get script
    5.     MarkerScript markerScript = go.GetComponent<MarkerScript>();
    6.  
    7.     // If found script
    8.     if (markerScript != null)
    9.     {
    10.         // If string matches
    11.         if (markerScript.markerAddress == "whatever")
    12.         {
    13.             // Do something
    14.         }
    15.     }
    16. }
    To communicate properly between scripts, check out Events and Delegates -
     
  3. MonkeyZero

    MonkeyZero

    Joined:
    Feb 20, 2013
    Posts:
    37
    @Stardog: Thank you so much for your help. I am very new to code. Know enough to do little stuff but still not good enough to just script on the fly. This helped alot thank.

    Once I get this into a stable working state I will release it all for free as I have said I would.