1
votes

I'm trying to find the distance between two objects using transform.position.

Here is my code for the scripts:

using UnityEngine;
using System.Collections;

public class Trigger : MonoBehaviour {

public GameObject Door;
public GameObject Player;

int count = 0;

// Use this for initialization
void Start () {
    Debug.Log (Door);
    Debug.Log (Player);
}

// Update is called once per frame
void Update () {
    Debug.Log (Door.transform.position);
    //Debug.Log (Door);
    //Debug.Log (Player);
    //float distance = Vector3.Distance (Door.transform.position, Player.transform.position);
    //Debug.Log (distance);
}

And here is an image showing 1) the scene and hierarchy 2) the inspector for the gameobject ive assigned the script to:

enter image description here

However the problem is now when i want to find the position of that object however it only returns the initial position - ie it's not updating the position of the gameObject player - how can i update the gameObject variable?

1
Do you see any errors in Trigger Start() method? - Skyblade
Your GameObject Door is not assigned to any instance of object. Start() method will not trigger a error since it will just print out NULL or something, but then you try to use Door as a instance and access the transform, the above error is thrown. - gin
I thought i had assigned the game object door to the game object in the scene (door) - as well as the gameObject player - Nightshade

1 Answers

1
votes
  • When I had the same error, the reason was that I had accidentally dragged the script to a wrong object in the scene view.

  • This can happen if you unintentionally attach your script to more than one game object and only it initialize it in one place.

I checked your Error image, you applied for Door object right. Put the script for WALLS or PLAYER. Will solve the issue