Find A GameObject's Position

Hi,

I made a PreFab for one of my entities in Unity. I call the entity a type of "power source". This power source or entity, sits on ground and does not move, ever. Meaning that its position will never change. The player (a primitive cube game object) does move, and if the user chooses, will eventually become close to the power source.

I have declared a public GameObject within the scope of my Player.cs Script (the cube that the user gets to move around with). What I want to do is that if the player gets close to the power source, the script will know that.

Basically, what I am after are the coordinates, x, z, of the power source. Keep in mind that I have a y-up system and the power source will never be above the ground, so that axis is not a factor in (as you might have guessed) my distance formula.

So, essentially, I need to grab the coordinates of the power source. How would I do that?

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    // For When Player comes within Range of the powerSource.
    public GameObject PowerSourcePreFab;

    public float MoveSpeed = 10;
    public float RotateSpeed = 40;

    void Start()
    {
        // Intialize each power souce to 0% as this is the start of the match.
    }

    // Update is called once per frame
    void Update () 
    {
        // Amount to Move
        float MoveForward = Input.GetAxis("Vertical")  * MoveSpeed * Time.deltaTime;
        float MoveRotate = Input.GetAxis("Horizontal") * RotateSpeed * Time.deltaTime;

        // Move the player
        transform.Translate(Vector3.forward * MoveForward);
        transform.Rotate(Vector3.up * MoveRotate);

    }
}

To reiterate, I simply need to access the coordinates of the power source entity in my game.

I did something like this once and when my player walked into a trigger. The trigger would make an object play an animation. I believe you can use the same code I used and just alter it a little.

I used the following code:

function movemaze1()
{
maze1 = true
audio.PlayOneShot(Mazemovesound);
var maze1 : GameObject = GameObject.Find("maze");
maze1.animation.Play("mazeonemovetwo")
}

Something's missing from your script, and you can't use your "`public GameObject PowerSourcePrefab`" like that. A prefab is just a template, it's not a reference to your "Power Source". It technically doesn't really have a "position".

You need to get references to all of the PowerSources in the scene, and iterate through those. Alternatively, you could (and this would be way easier), just place a script on each PowerSource to check if the player is in range, and then do something at that point in time.

Here's some pseudocode for a script you could place on your PowerSources:

// Pseudocode

float distance

// store the player object reference in here from the SCENE
// using the editor inspector
GameObject player

function Update
   if transform.position - player.position is less than distance then
      // do something here
   end if
end function