InGame search function

In space there is great distances between planets…
You can barely see them since the stars make the planets “fade” in as a “star”

Same goes for my space game.
So my question is:

How do you make a in game search function that you can access via a klick button to type for example “Jupiter”,
and then it search for a object with tag “Jupiter”, and when it find the planet a button shows up that if you klick on turns your space ship to point at the object “jupiter”

I guess this sounds messy…but this is the best way i can explain it.

You basically are creating a filter.

That button needs to have a stored string value of say “Jupiter” And then you’ll have a list of planets I assume.

Iterate through the list, and see if the current planet in the list, matches the name Jupiter.

I see!

Sounds advanced some how, you got any links to some tutorials for that? or could you please maybe help me with a example to work on?

Say you have a list of planets. Those are gameObjects.

List planetList = new List();

Now that you have a list, fill it in the inspector, and when you press that button, call a function that passes in a string parameter.

Take that string and do a foreach loop to parse it.

void SearchPlanet(string planetName)
{
foreach(var planet in planetList)
{
     if (planet.name == planetName)
     {
            //You have a match! Now do stuff
     }
}
}

I think a generic Dictionary might be a better approach than a generic List. string/gameobject key pair for all the things in the game you want to be able to search for.

I dont know if im on the right way here with the code? but im getting error

The type or namespace name `List`1' could not be found. Are you missing a using directive or an assembly reference?
using UnityEngine;
using System.Collections;

public class PlanetSearch : MonoBehaviour {


    private GameObject planetList = new List<GameObject>();
    // Use this for initialization
    void Start () {
  
    }


    void SearchPlanet(string planetName)
    {
        foreach(var planet in planetList)
        {
            if (planet.name == planetName)
            {
                //You have a match! Now do stuff
            }
        }
    }

You need to make your list a list.

public List<GameObject> planetList = new List<GameObject>();

Also, at the top, include :

using System.Collections.Generic; //This gives you access to the lists lib.

I have a video in my tutorial series (link in signature) on Lists. You can check it out there for better understanding. Also, I have a full fundamentals, intermediate, and 30% advanced series on my playlist. (Advance playlist is still on going) I recommend checking it out. It will help you

Oh! i deleted “List” when i typed “private” that should been “public” lol.

Now i just need to figure out how to make that canvas button appear when i activate the “PlanetCatalog” image + a function to be able to type and execute the searhing.

I had some trouble with that canvas button earlier today since it have a “image, button, text” script that must be checked/unchecked at same time on button klick . Any tips?

I will take a look at your tutorials, thanks for that tip!