Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make this script only work when close enough to gameobject?

Discussion in 'Scripting' started by Treasureman, Nov 3, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code that changes a certain UI Image's color when you mouse cursor is hovered over an assigned Collider. This is the code...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class BoatCommandAppear: MonoBehaviour {
    6.    
    7.     public Image myText;
    8.     public float fadeTime;
    9.     public bool displayInfo;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.         myText = GameObject.Find ("Boat Command").GetComponent<Image> ();
    15.         myText.color = Color.clear;
    16.         //Screen.showCursor = false;
    17.         //Screen.lockCursor = true;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.        
    24.         FadeText ();
    25.        
    26.         /*if (Input.GetKeyDown (KeyCode.Escape))
    27.        
    28.                 {
    29.                         Screen.lockCursor = false;
    30.                        
    31.                 }
    32.                 */
    33.        
    34.        
    35.     }
    36.    
    37.     void OnMouseOver()
    38.     {
    39.         displayInfo = true;
    40.        
    41.     }
    42.    
    43.    
    44.    
    45.     void OnMouseExit()
    46.        
    47.     {
    48.         displayInfo = false;
    49.        
    50.     }
    51.    
    52.    
    53.     void FadeText ()
    54.        
    55.     {
    56.        
    57.        
    58.         if(displayInfo)
    59.         {
    60.            
    61.             myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
    62.         }
    63.        
    64.         else
    65.         {
    66.            
    67.             myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
    68.         }
    69.        
    70.        
    71.        
    72.        
    73.     }
    74.    
    75.    
    76.    
    77. }
    But, I'm using this as an interaction script (An 'E' appears when you hover over it, signifying it can be interacted with). I want to figure out how to add another input to this script so that you also have to be close enough to the collider to make the color change. I know it would probably involve some raycasting, but I have no idea how raycasting works. What could I add to this script t get my desired effect?
     
  2. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    Hi Treasureman,

    There's a few ways you can do distance, three of the most common ways would be to use: Raycasting, Vector3.DIstance or OnTriggerEnter().
    I personally use a mix of the three, depending on the situation, the most simple solution here is to use OnTriggerEnter.

    Let me know if you need further assistance.
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Im no pro but you could do something like:
    Just add these parts into your script and drag the Collider for the UI object you changing the colour of and add the player Gameobejct.
    DISCLAIMER:
    I am definitely no pro, just a friendly guy trying to help.
    Code (CSharp):
    1. public GameObject Player;
    2. public Collider UIObject;
    3.  
    4. public float distance;
    5.  
    6. void Update()
    7. {
    8.     distance = vector3.Distance (Player.transform.position, UIObject.transform.position;
    9. }
    10. if (distance <= 50)
    11. {
    12.     //Change colour here
    13. }
     
    Last edited: Nov 3, 2015
  4. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, to be honest with you, I'm completely illiterate in CSharp. I just need to know where I would put whatever I need to put into the script. Sorry to be a trouble
     
  5. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    This is pretty close, thanks for helping out TaleOf4Gamers =)

    If you're not declaring, you need to make sure the code is inside of method or you will get errors :D

    Code (CSharp):
    1. public GameObject Player;
    2. public Collider UIObject;
    3.  
    4. public float distance;
    5.  
    6. private BoatCommandAppear boatCommandAppear;
    7.  
    8. void Start() {
    9. boatCommandAppear = FindObjectOfType<BoatCommandAppear>(); // Make sure this script is in the scene and has initialized first!
    10. }
    11. void Update()
    12. {
    13.     distance = vector3.Distance (Player.transform.position, UIObject.transform.position;
    14.  
    15. if (distance <= 50)
    16. {
    17.     //This will be called A LOT once in range.. So it isn't the best solution here.
    18. boatCommandAppear.InRange = true; // Make sure you declare this bool in your boatCommandAppear script!
    19. } else {
    20. boatCommandAppear.InRange = false;
    21. }
    22. }
    23.  
    Now the problem with this is that it will be called multiple times per second, so for this case isn't the best idea, but it will do the job until you explore OnTriggerEnter or Raycasting! Just create a new class and copy/paste this in.
     
    TaleOf4Gamers likes this.
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I think I wrote it right, but I get a few errors. I'm guessing it's just because I'm missing some brackets or something, but I'm not entirely sure (I wrote it into a different script, so instead of boat command, its flairgun command)...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class FlairgunCommandAppear: MonoBehaviour {
    6.  
    7.     public Image myText;
    8.     public float fadeTime;
    9.     public bool displayInfo;
    10.     public GameObject Player;
    11.     public Collider UIObject;
    12.     public float distance;
    13.  
    14.     private FlairgunCommandAppear commandAppear;
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.         commandAppear = FindObjectOfType<BoatCommandAppear>();
    21.      
    22.         myText = GameObject.Find ("Flair Gun Command").GetComponent<Image> ();
    23.         myText.color = Color.clear;
    24.         //Screen.showCursor = false;
    25.         //Screen.lockCursor = true;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update ()
    30.     {
    31.     {
    32.         {
    33.         distance = Vector3.Distance (Player.transform.position, UIObject.transform.position);
    34.  
    35.         if (distance <= 50)
    36.         {
    37.             //This will be called A LOT once in range.. So it isn't the best solution here.
    38.             commandAppear.InRange = true; // Make sure you declare this bool in your boatCommandAppear script!
    39.         } else {
    40.             commandAppear.InRange = false;
    41.         }
    42.     }
    43.  
    44.         FadeText ();
    45.      
    46.         /*if (Input.GetKeyDown (KeyCode.Escape))
    47.      
    48.                 {
    49.                         Screen.lockCursor = false;
    50.                      
    51.                 }
    52.                 */
    53.      
    54.      
    55.     }
    56.  
    57. void OnMouseOver()
    58.     {
    59.         displayInfo = true;
    60.      
    61.     }
    62.  
    63.  
    64.  
    65.         void OnMouseExit();
    66.      
    67.     {
    68.         displayInfo = false;
    69.      
    70.     }
    71.  
    72.  
    73.     void FadeText ()
    74.      
    75.     {
    76.      
    77.      
    78.         if(displayInfo)
    79.         {
    80.  
    81.             myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
    82.         }
    83.      
    84.         else
    85.         {
    86.          
    87.             myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
    88.         }
    89.      
    90.      
    91.      
    92.      
    93.     }
    94.  
    95.  
    96.  
    97. }
    Here are the errors I got.

    Assets/UI/FlairgunCommandAppear.cs(57,16): error CS1547: Keyword `void' cannot be used in this context
    Assets/UI/FlairgunCommandAppear.cs(57,17): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
    Assets/UI/FlairgunCommandAppear.cs(67,9): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
    Assets/UI/FlairgunCommandAppear.cs(68,29): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
    Assets/UI/FlairgunCommandAppear.cs(73,14): error CS0116: A namespace can only contain types and namespace declarations

    I just need to know what I left out.
     
    Last edited: Nov 5, 2015
  7. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
    Looks like you have an extra open bracket at line 30 and an extra function header with no return variable definition at line 44.
     
  8. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I did some editing, but still left with 3 errors. I'm not sure why the the FadeText doesn't work. It worked fine in my other scripts. Here's my revised version of my last code...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class FlairgunCommandAppear: MonoBehaviour {
    6.  
    7.     public Image myText;
    8.     public float fadeTime;
    9.     public bool displayInfo;
    10.     public GameObject Player;
    11.     public Collider UIObject;
    12.     public float distance;
    13.    
    14.     private FlairgunCommandAppear commandAppear;
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.         commandAppear = FindObjectOfType<BoatCommandAppear>();
    21.        
    22.         myText = GameObject.Find ("Flair Gun Command").GetComponent<Image> ();
    23.         myText.color = Color.clear;
    24.         //Screen.showCursor = false;
    25.         //Screen.lockCursor = true;
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update ()
    30.         {
    31.  
    32.         FadeText ();
    33.  
    34.         distance = Vector3.Distance (Player.transform.position, UIObject.transform.position);  
    35.  
    36.         if (distance <= 50)
    37.         {
    38.             //This will be called A LOT once in range.. So it isn't the best solution here.
    39.             commandAppear.InRange = true; // Make sure you declare this bool in your boatCommandAppear script!
    40.         } else {
    41.             commandAppear.InRange = false;
    42.         }
    43.    
    44.  
    45.         }
    46.        
    47.         /*if (Input.GetKeyDown (KeyCode.Escape))
    48.        
    49.                 {
    50.                         Screen.lockCursor = false;
    51.                        
    52.                 }
    53.                 */
    54.        
    55.        
    56.  
    57.    
    58. void OnMouseOver()
    59.     {
    60.         displayInfo = true;
    61.        
    62.     }
    63.    
    64.    
    65.    
    66.         void OnMouseExit();
    67.        
    68.     {
    69.     displayInfo = false;
    70.        
    71.     }
    72.    
    73.    
    74.     void FadeText ()
    75.        
    76.     {
    77.        
    78.        
    79.         if(displayInfo)
    80.         {
    81.  
    82.             myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
    83.         }
    84.        
    85.         else
    86.         {
    87.            
    88.             myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
    89.         }
    90.        
    91.        
    92.        
    93.        
    94.     }
    95.    
    96.    
    97.    
    98.  
    And I still get these errors...

    Assets/UI/FlairgunCommandAppear.cs(68,9): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
    Assets/UI/FlairgunCommandAppear.cs(69,21): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
    Assets/UI/FlairgunCommandAppear.cs(74,14): error CS0116: A namespace can only contain types and namespace declarations

    Here's one of the scripts I have where FadeText (); works fine...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class BoatCommandAppear: MonoBehaviour {
    6.    
    7.     public Image myText;
    8.     public float fadeTime;
    9.     public bool displayInfo;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.         myText = GameObject.Find ("Boat Command").GetComponent<Image> ();
    15.         myText.color = Color.clear;
    16.         //Screen.showCursor = false;
    17.         //Screen.lockCursor = true;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.        
    24.         FadeText ();
    25.        
    26.         /*if (Input.GetKeyDown (KeyCode.Escape))
    27.        
    28.                 {
    29.                         Screen.lockCursor = false;
    30.                        
    31.                 }
    32.                 */
    33.        
    34.        
    35.     }
    36.    
    37.     void OnMouseOver()
    38.     {
    39.         displayInfo = true;
    40.        
    41.     }
    42.    
    43.    
    44.    
    45.     void OnMouseExit()
    46.        
    47.     {
    48.         displayInfo = false;
    49.        
    50.     }
    51.    
    52.    
    53.     void FadeText ()
    54.        
    55.     {
    56.        
    57.        
    58.         if(displayInfo)
    59.         {
    60.            
    61.             myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
    62.         }
    63.        
    64.         else
    65.         {
    66.            
    67.             myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
    68.         }
    69.        
    70.        
    71.        
    72.        
    73.     }
    74.    
    75.    
    76.    
    77. }
    Thanks if you can help!