- Home /
C# script for calculating the distance between the player and objects
using UnityEngine;
using System.Collections;
public class PdDataHandler : MonoBehaviour {
public float WhitePetalFlowerDist;
public float BluePetalFlowerDist;
public float RedPetalFlowerDist;
public float WhiteRoundFlowerDist;
public float YellowRoundFlowerDist;
public float PinkRoundFlowerDist;
void Start () {
}
void Update () {
WhitePetalFlowerDist = Vector3.Distance (Player.transform.position, WhitePetalFlower.transform.position);
print("Distance to WhitePetalFlower: " + WhitePetalFlowerDist);
BluePetalFlowerDist = Vector3.Distance (Player.transform.position, BluePetalFlower.transform.position);
print("Distance to BluePetalFlower: " + BluePetalFlowerDist);
RedPetalFlowerDist = Vector3.Distance (Player.transform.position, RedPetalFlower.transform.position);
print("Distance to RedPetalFlower: " + RedPetalFlowerDist);
WhiteRoundFlowerDist = Vector3.Distance (Player.transform.position, WhiteRoundFlower.transform.position);
print("Distance to WhiteRoundFlower: " + WhiteRoundFlowerDist);
YellowRoundFlowerDist = Vector3.Distance (Player.transform.position, YellowRoundFlower.transform.position);
print("Distance to YellowRoundFlower: " + YellowRoundFlowerDist);
PinkRoundFlowerDist = Vector3.Distance (Player.transform.position, PinkRoundFlower.transform.position);
print("Distance to PinkRoundFlower: " + PinkRoundFlowerDist);
}
}
Hi there! I'm new to Unity and having trouble figuring out this issue, there is clearly a huge gap in my understanding.
What I'm trying to do is to create "distance floats" by calculating the distance between the 6 game objects and my player object.
I then want my PdDataHandler Script to print these to the console.
If this seems silly or counter-intuitive to anyone, please push me in the right direction!!
My googling revealed that the problem is related to attempting to call on things that are a part of a class instead of an object but I still don't really understand. Any help would be greatly appreciated.
Answer by BadmanMassive · Mar 22, 2015 at 07:30 AM
using UnityEngine;
using System.Collections;
public class PdDataHandler : MonoBehaviour {
private float WhitePetalFlower_Distance = 0.0f;
private float BluePetalFlower_Distance = 0.0f;
private float RedPetalFlower_Distance = 0.0f;
private float WhiteRoundFlower_Distance = 0.0f;
private float YellowRoundFlower_Distance = 0.0f;
private float PinkRoundFlower_Distance = 0.0f;
public Transform Player;
public Transform WhitePetalFlower;
public Transform BluePetalFlower;
public Transform RedPetalFlower;
public Transform WhiteRoundFlower;
public Transform YellowRoundFlower;
public Transform PinkRoundFlower;
void Start () {
OSCHandler.Instance.Init();
}
void Update()
{
WhitePetalFlower_Distance = Vector3.Distance(Player.position, WhitePetalFlower.position);
print("Distance to WhitePetaFlower: " + WhitePetalFlower_Distance);
BluePetalFlower_Distance = Vector3.Distance(Player.position, BluePetalFlower.position);
print("Distance to BluePetalFlower: " + BluePetalFlower_Distance);
RedPetalFlower_Distance = Vector3.Distance(Player.position, RedPetalFlower.position);
print("Distance to RedPetalFlower: " + RedPetalFlower_Distance);
WhiteRoundFlower_Distance = Vector3.Distance(Player.position, WhiteRoundFlower.position);
print("Distance to WhiteRoundFlower: " + WhiteRoundFlower_Distance);
YellowRoundFlower_Distance = Vector3.Distance(Player.position, YellowRoundFlower.position);
print("Distance to YellowRoundFlower: " + YellowRoundFlower_Distance);
PinkRoundFlower_Distance = Vector3.Distance(Player.position, PinkRoundFlower.position);
print("Distance to PinkRoundFlower: " + PinkRoundFlower_Distance);
}
}
Solved it! This seems to work!
Your answer
Follow this Question
Related Questions
ERROR - An object reference is required to access non-static member 3 Answers
variable = true from another script 2 Answers
Static variables not working inside Function Update() 1 Answer
Static Variable Problem 1 Answer
Public and Static Variables 2 Answers