Question calling object items in code

Hey so I’m wondering how to call an object from game in script, e.g. calling cube1 with script to test parameter of cube1?

Not 100% sure that I follow your question. You can’t really “call an object”; you can call a method on an object… is that what you mean? For that, you need a reference to said object, and just call its method: “objRef.SomeMethod()”.

Please explain a bit more if you meant something else. :wink:

Generally you would call a method in a component on a gameobject, which if set to public you can do from any other component script on any other gameobject that has a reference to it. The part you need to think about is how you want to acquire and store the reference. There’s lots of ways to do that, from just dragging the gameobject or component to a variable in the inspector, to getting a reference as a result of some callback like during a physics collision, or using one of the various Find methods.

Example where you have a gameobject tagged with “Player” and has a script called “PlayerScript” which has a method called DoSomething():

GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
if (playerObject != null)
{
    PlayerScript ps = playerObject.GetComponent<PlayerScript>();
    if (ps != null)
    {
        ps.DoSomething();
    }
}

It isn’t required to always check everything for null before using it, but it helps avoid accidental null reference errors, and you can easily add Debug.Log lines if anything is null to catch the issue before it drives you crazy why something isn’t working.

That’s great thank you, I’m planning to sort out the problem of standing on a moving platform and I understand you need to parent the object to each other once on it.