Understanding using components

Im having a problem understanding how to access data or change data in components using a script.
I attached a screen shot of my hierarchy

i have Time a UI Canvas
and a UI Text called WorldTime which is a text ui
but i just cant update it via a script

here is my script

    public GameObject GameOb;

    // Update is called once per frame
    void Update()
    {
        GameOb.GetComponent<WorldTime>().text = "ok";
            }
}

but no matter what i try i get errors

please help

5612644--581215--screen.PNG

The code should work if GameOb has a WorldTime component on it, and you have click-dragged a GameObject into the GameOb inspector slot.

The <WorldTime> is the name of the component, not the GameObject.

Try public Text GameOb; with GameOb.GetComponent<Text>.text = "ok";.

Add using UnityEngine.UI; to the top of the script.

ok thats working thanks
this line
public Text GameOb, i dont really understand what its doing?

It means create a variable named “GameOb”, of type “Text”. The name can be anything you want. The type has to be the class/component you are trying to store there.

Note: in the original code you posted it is public GameObject GameOb;. This is to declare a variable named GameOb of type GameObject, which is different from a Text object.

thanks guys, where can i get a list of all the objects available? are there many?

What do you mean by a list of all objects available? Just like with ‘normal’ object oriented programming (in case you are familiar), basically everything is an object. You can also create your own object classes. The only difference is that Unity offers its API to you, so you can use all the pre-existing types.
There are also a couple ‘special’ objects due to the Unity workflow:

A ‘GameObject’ is a container which has at least one ‘Transform’ component (storing the data about position, rotation, scale and parenting), and can have other components. These components are, for the most part, scripts deriving from Unitys’ Monobehaviour type. Everything you see in your project hierarchy is a GameObject. If you click on them you will see that each of them has a Transform, and some of them (like the camera for example) have other components.

This is the basis that makes up the hierarchy you see in Unity. GameObjects with Transforms and other components.
As for components, there are thousands, premade by Unity to be used by you. There certainly is a list of all of them, but i dont think it would be of much use. On top of this you can write as many custom ones as you want to. Every script you write which derives from Monobehaviour, can then be added as a component to a GameObject. The script you posted above is one of those as well.

Great explanation thanks.

^ This… I agree it would not be much use to you @warrenbrandt . Over time you will develop a bunch of things that will help you quickly find what you need. Some of those things are:

  • learning the correct terminology to describe what you want to do

  • learning which API your solution parts might be in (UnityEngine API, or part of the core .NET API for instance)

Just keep chipping away at your projects and when you learn something new, it’s always good to go to the documentation for it and look up what else it might do.