Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BadmanMassive · Mar 22, 2015 at 08:00 AM · variablebeginnerfunctionstaticobject reference

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges