• 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 henrypuspurs · Jun 13, 2013 at 03:34 PM · c#gui

Checking for Player to Object distance for other scripts to check state.

Im new to unity and scripting and managed to make a script for clicking on an object to mute/unmute it, but only at a close range. The problem I have now is Im trying to seperate them so that one script checks the distance, another mutes/unmutes, and another to change the GUI crosshair colour. I should tell you in advance I only started to learn scripting of any kind yesterday.

T$$anonymous$$s is the script to check distance: using UnityEngine; using System.Collections;

 public class muteaudioclick : MonoBehaviour
 {
     public int triggdist = 3;
     
     public Transform player;
     
     private int distance;
     
     public bool playerclose;
     
     void Onupdate(){
         
         float distance = Vector3.Distance(player.position, transform.position);
         if(distance < triggdist)
         {
         playerclose = true;
         }
         else
         {
         playerclose = false;
         }
     }
 }

T$$anonymous$$s is for muting

 using UnityEngine;
 using System.Collections;
 
 public class muteaudioclick : MonoBehaviour
 {
     
     private checkplayerdist playerclose;
     
     void Awake()
     {
         playerclose = GetComponent<checkplayerdist>();
     }
             
     
     void OnMouseDown()
     {
         if(playerclose == true)
         {
     
         
         if(audio.mute == false)
         
             {
                 audio.mute = true;
                 Debug.Log ("Sound Off");
             }
         else
             {
                 audio.mute = false;
                 Debug.Log ("Sound On");
             }
         }
     }
 }

And finally the crosshair, I havent even tried to incorperate t$$anonymous$$s yet, trying to get the mute function to work.

 using UnityEngine;
 using System.Collections;
 
 public class CrosshairGUI : MonoBehaviour
 {
     
     void OnGUI()
     {
         GUI.color = Color.w$$anonymous$$te;
         GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "X");
     }
 }
Comment
Add comment · Show 3
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
avatar image henrypuspurs · Jun 13, 2013 at 07:42 PM 0
Share
avatar image Graham-Dunnett ♦♦ · Jun 13, 2013 at 07:44 PM 0
Share
avatar image henrypuspurs · Jun 13, 2013 at 08:08 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by AntiLunchBox · Jun 13, 2013 at 08:23 PM

you want to change OnUpdate to just Update,

and you want to change 'if (playerclose == true)' to 'if(playerclose.playerclose)'

  1. theres no need to check == true, since its already a boolean. if it == true, itll just pass

  2. You are referencing the script, not the boolean in the script

you may want to change the name of: 'private checkplayerdist playerclose;' to make it more clear.

It would be:

private checkplayerdist playerDistScript;

and

if(playerDistScript.playerclose)

Comment
Add comment · Show 5 · 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
avatar image henrypuspurs · Jun 13, 2013 at 08:52 PM 0
Share
avatar image AntiLunchBox · Jun 13, 2013 at 09:03 PM 0
Share
avatar image henrypuspurs · Jun 13, 2013 at 09:03 PM 0
Share
avatar image AntiLunchBox · Jun 13, 2013 at 09:07 PM 0
Share
avatar image henrypuspurs · Jun 13, 2013 at 09:10 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to display the damage value from a mob 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Problems with GUI and Resolutions. (C#) 1 Answer

How to display Int variable onto the GUI/HUD 0 Answers

Is there anyway to only show a part of a GUI image during the game? (C#) 1 Answer


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