Make this script only work when close enough to gameobject?

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…

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BoatCommandAppear: MonoBehaviour {
   
    public Image myText;
    public float fadeTime;
    public bool displayInfo;
   
    // Use this for initialization
    void Start () {
       
        myText = GameObject.Find ("Boat Command").GetComponent<Image> ();
        myText.color = Color.clear;
        //Screen.showCursor = false;
        //Screen.lockCursor = true;
    }
   
    // Update is called once per frame
    void Update ()
    {
       
        FadeText ();
       
        /*if (Input.GetKeyDown (KeyCode.Escape))
        
                {
                        Screen.lockCursor = false;
                        
                }
                */
       
       
    }
   
    void OnMouseOver()
    {
        displayInfo = true;
       
    }
   
   
   
    void OnMouseExit()
       
    {
        displayInfo = false;
       
    }
   
   
    void FadeText ()
       
    {
       
       
        if(displayInfo)
        {
           
            myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
        }
       
        else
        {
           
            myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
        }
       
       
       
       
    }
   
   
   
}

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?

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.

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.

public GameObject Player;
public Collider UIObject;

public float distance;

void Update()
{
    distance = vector3.Distance (Player.transform.position, UIObject.transform.position;
}
if (distance <= 50)
{
    //Change colour here
}

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

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 :smile:

public GameObject Player;
public Collider UIObject;

public float distance;

private BoatCommandAppear boatCommandAppear;

void Start() {
boatCommandAppear = FindObjectOfType<BoatCommandAppear>(); // Make sure this script is in the scene and has initialized first!
}
void Update()
{
    distance = vector3.Distance (Player.transform.position, UIObject.transform.position;

if (distance <= 50)
{
    //This will be called A LOT once in range.. So it isn't the best solution here.
boatCommandAppear.InRange = true; // Make sure you declare this bool in your boatCommandAppear script!
} else {
boatCommandAppear.InRange = false;
}
}

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.

1 Like

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)…

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class FlairgunCommandAppear: MonoBehaviour {

    public Image myText;
    public float fadeTime;
    public bool displayInfo;
    public GameObject Player;
    public Collider UIObject;
    public float distance;
 
    private FlairgunCommandAppear commandAppear;


    // Use this for initialization
    void Start () {

        commandAppear = FindObjectOfType<BoatCommandAppear>();
     
        myText = GameObject.Find ("Flair Gun Command").GetComponent<Image> ();
        myText.color = Color.clear;
        //Screen.showCursor = false;
        //Screen.lockCursor = true;
    }
 
    // Update is called once per frame
    void Update ()
    {
    {
        {
        distance = Vector3.Distance (Player.transform.position, UIObject.transform.position); 

        if (distance <= 50)
        {
            //This will be called A LOT once in range.. So it isn't the best solution here.
            commandAppear.InRange = true; // Make sure you declare this bool in your boatCommandAppear script!
        } else {
            commandAppear.InRange = false;
        }
    }

        FadeText ();
     
        /*if (Input.GetKeyDown (KeyCode.Escape))
      
                {
                        Screen.lockCursor = false;
                      
                }
                */
     
     
    }
 
void OnMouseOver()
    {
        displayInfo = true;
     
    }
 
 
 
        void OnMouseExit();
     
    {
        displayInfo = false;
     
    }
 
 
    void FadeText ()
     
    {
     
     
        if(displayInfo)
        {

            myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
        }
     
        else
        {
         
            myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
        }
     
     
     
     
    }
 
 
 
}

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.

Looks like you have an extra open bracket at line 30 and an extra function header with no return variable definition at line 44.

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…

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class FlairgunCommandAppear: MonoBehaviour {

    public Image myText;
    public float fadeTime;
    public bool displayInfo;
    public GameObject Player;
    public Collider UIObject;
    public float distance;
   
    private FlairgunCommandAppear commandAppear;


    // Use this for initialization
    void Start () {

        commandAppear = FindObjectOfType<BoatCommandAppear>();
       
        myText = GameObject.Find ("Flair Gun Command").GetComponent<Image> ();
        myText.color = Color.clear;
        //Screen.showCursor = false;
        //Screen.lockCursor = true;
    }
   
    // Update is called once per frame
    void Update ()
        {

        FadeText ();

        distance = Vector3.Distance (Player.transform.position, UIObject.transform.position);   

        if (distance <= 50)
        {
            //This will be called A LOT once in range.. So it isn't the best solution here.
            commandAppear.InRange = true; // Make sure you declare this bool in your boatCommandAppear script!
        } else {
            commandAppear.InRange = false;
        }
   

        }
       
        /*if (Input.GetKeyDown (KeyCode.Escape))
        
                {
                        Screen.lockCursor = false;
                        
                }
                */
       
       

   
void OnMouseOver()
    {
        displayInfo = true;
       
    }
   
   
   
        void OnMouseExit();
       
    {
    displayInfo = false;
       
    }
   
   
    void FadeText ()
       
    {
       
       
        if(displayInfo)
        {

            myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
        }
       
        else
        {
           
            myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
        }
       
       
       
       
    }

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…

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BoatCommandAppear: MonoBehaviour {
   
    public Image myText;
    public float fadeTime;
    public bool displayInfo;
   
    // Use this for initialization
    void Start () {
       
        myText = GameObject.Find ("Boat Command").GetComponent<Image> ();
        myText.color = Color.clear;
        //Screen.showCursor = false;
        //Screen.lockCursor = true;
    }
   
    // Update is called once per frame
    void Update ()
    {
       
        FadeText ();
       
        /*if (Input.GetKeyDown (KeyCode.Escape))
        
                {
                        Screen.lockCursor = false;
                        
                }
                */
       
       
    }
   
    void OnMouseOver()
    {
        displayInfo = true;
       
    }
   
   
   
    void OnMouseExit()
       
    {
        displayInfo = false;
       
    }
   
   
    void FadeText ()
       
    {
       
       
        if(displayInfo)
        {
           
            myText.color = Color.Lerp (myText.color, Color.white, fadeTime * Time.deltaTime);
        }
       
        else
        {
           
            myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
        }
       
       
       
       
    }
   
   
   
}

Thanks if you can help!