Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I need help with this

Discussion in 'Scripting' started by Balocka, May 29, 2022.

  1. Balocka

    Balocka

    Joined:
    May 28, 2022
    Posts:
    2
    I'm not sure how to use an game object's position as a vector2 in a function im trying to make for the players first spawn point before checkpoints.

    This is the script.

    it works just fine if I put something like 1, 1 where i want to use the position of my game object.

    public class CheckPointTracker : MonoBehaviour
    {

    private void Awake()
    {
    GameObject.FindGameObjectWithTag("Player").transform.position = CheckPoint;
    }

    public static Vector2 CheckPoint = new Vector2(IN GAME OBJECT HERE);
    }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Code (CSharp):
    1. public Vector2 Checkpoint { get { return new Vector2(transform.position.x, transform.position.y);
    You don't want it to be static, and best to make it a property.
     
  3. Balocka

    Balocka

    Joined:
    May 28, 2022
    Posts:
    2
    well checkpoint is from another script so it changes after the player goes into a differant checkpoint the problem is that i want to make a gameobject the first spawnpoint but i dont know how to use a gameobject's position as a vector2
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    I mean, I did literally just show you how to do so.
     
    Kurt-Dekker likes this.