Accessing variables and displaying them in a GUI

Hello Community,

I am totally new to Unity (yesterday i installed it) and I’m trying to create a little solar system with a window that shows me all properties of the planet i was selecting.

Now i was wondering how it must be implemented in Unity. My first idea was to make a class like this…

using UnityEngine;
using System.Collections;

public class PlanetProperties : MonoBehaviour {
    public enum PlanetType {
        IcePlanet,
        SandPlanet,
        DustPlanet,
        GasPlanet,
        WaterPlanet,
    }

    public PlanetType planetType;
    public Vector2 averageTemperature; //Will be replaced with a custom Class
}

…and then add this Component to all planets.

Then for the selection of a planet, i will implement a class like this:…

using UnityEngine;
using System.Collections;

public class SelectableObject : MonoBehaviour {

    void OnMouseDown(){

    }
}

…so every Planet is selectable. But now, how i can feed the GUI with the values of the planet I’ve selected?

Thanks for every herlp :slight_smile:

A quick and dirty way.

public class PlanetUI : MonoBehaviour
{
    public PlanetProperties SelectedPlanet;

    void OnGUI()
    {
        if (SelectedPlanet != null)
            // draw your UI
    }
}
public class SelectableObject : MonoBehaviour
{
    void OnMouseDown()
    {
        FindObjectOfType<PlanetUI>().SelectedPlanet = GetComponent<PlanetProperties>();
    }
}

Works very well, thank you :wink: But how do i implement, if the user clicks in a free space, so no planet is selected?

Remove your OnMouseDown method and instead do a raycast out from the camera at the mouse cursor’s position. Cameras have methods for converting screen space to world space and the raycasting documentation is pretty robust.