Finding the position of an object in vsc

Im trying to get the position of a 3d object in my code but i’m stuck trying to set it up, what can i do to fix it?

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Threading;

// counts down from a number, and ends the game when time runs out

public class Timer : MonoBehaviour
{
    public TextMeshProUGUI timerText;
    public float remainingTime;
    public GameObject gameObjectToMove;
    public GameObject cubePosition;

    void Update()
    {
        Canvas canvas = FindObjectOfType<Canvas>();
        float GameObjectPositionX = cubePosition.x;
        float GameObjectPositionY;
        if (remainingTime > 0)
        {
            remainingTime -= Time.deltaTime;
            if (remainingTime <= 3)
            {
                timerText.color = Color.red;
            }
        }
        else if (remainingTime < 0)
        {
            remainingTime = 0;
            Time.timeScale = 0;
            gameObjectToMove.transform.position = new Vector3(GameObjectPositionX / 2, GameObjectPositionY / 2, 0);
        }
        int minutes = Mathf.FloorToInt(remainingTime / 60);
        int seconds = Mathf.FloorToInt(remainingTime % 60);

        timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

Then I think we all agree here that you should unstick yourself!

If you want complete strangers to help you, try this format:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

If you aren’t sure what is happening, imagine how unsure WE are!

In that case, it is time for you to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Sorry this is my first time posting on unity discussions, I didn’t know about those services :frowning:

Don’t sweat it for now, you don’t know what you don’t know. I think Kurt was giving you pointers to get more useful help next time, as your question is vague and we’re going to have to guess at where you need help before we can have a go at giving it to you.

My guess is this: you have a variable called “cubePosition” which is of type GameObject. A GameObject does not directly contain a position, so the code isn’t even running. Later on, you have this…

float GameObjectPositionX = cubePosition.x;

I assume you’re getting a compiler error in your Console window for that line, because there isn’t a variable called ‘x’.

If you select a GameObject in your Scene view and then take a look in the Inspector tab, you’ll see that the first Component on any GameObject is a “Transform”. That Transform Component is what gives a GameObject its relationship to the Scene, including its position. So, to read a GameObject’s position you need to access not just the GameObject, but the Transform inside it.

To do that in code:

Vector3 position = myGameObject.transform.position;

(Because every GameObject has a Transform there’s a special property to access it. For most other components you would use GetComponent(…) instead, which you can look up in the manual.)

Do make sure that you read the manual sections on GameObjects, Components and Transforms. There’s a lot of info there which is absolutely fundamental to successfully working in Unity. It’s simple and flexible once you grok it, but it’s tricky for a newbie as there’s a few interacting concepts and it’ll seem pretty opaque until they’ve all clicked.

Separately, do you have the Console tab open in Unity? If not, open it now. Click the “Clear” button. Any lines marked in red after that are compiler errors, which need to be solved before your new code will compile and be run. Always solve them in the order they are written (because later ones are often symptoms of earlier ones). Error messages seem cryptic at first, but they’re super helpful once you can read them.

1 Like