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. Dismiss Notice

SetActive() working on one icon, not the other. Exact same code

Discussion in 'Scripting' started by mmancuso666, Aug 1, 2022.

  1. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    I have two icons on my screen. When I hover over them, I want a Canvas GameObject to be SetActive(true). When I move the mouse away from the icon, I set the Canvas GameObject to SetActive(false);

    Two icons, with exactly the same code.

    One icon performs as planned. The other does not respond to the SetActive(true) command.

    The icons are well separated from each other.

    Here's the code:
    Code (csharp):
    1.  
    2.     private void Update()
    3.     {
    4.         Vector3 mPos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
    5.  
    6.         if ((mPos.x > transform.position.x - offset) && (mPos.y > transform.position.y - offset) && (mPos.x < transform.position.x + offset) && (mPos.y < transform.position.y + offset))
    7.         {
    8.             Debug.Log("Over the icon");
    9.             infoBackground.SetActive(true);
    10.             DisplayInfo();
    11.  
    12.         }
    13.         else
    14.         {
    15.             infoBackground.SetActive(false);
    16.  
    17.         }
    18.     }
    19.  
    Any help would be appreciated.
     
    Last edited: Aug 1, 2022
  2. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    One other note. The debug statement "Over the icon" IS triggered on both icons.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Well, first, I would suggest using something like OnPointerEnter and OnPointerExit instead of what you have. I would think it'd be much easier.

    Next, do both of these target the same infoBackground gameObject? If so, what is likely happening is Icon 1 and Icon 2 run their updates in a certain order.

    So, let's say Icon 2 runs first, then Icon 1 runs.
    When you mouse over Icon 1, Icon 2 turns off infoBackground due to the else. Then Icon 1 turns it on. Thus visually, it should appear on.

    Then when you mouse over Icon 2. Icon 2 turns on infoBackground and Icon 1 turns it off, thus visually it should appear off.

    That is my guess as to what is happening, which is why I suggest a different method.
     
    Last edited: Aug 1, 2022
    TheDevloper and mopthrow like this.
  4. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    I'll check out the OnPointerEnter code.

    The icons are referencing the same infoBackground.

    They are fairly far apart, and even if I wait a second or two before hovering over the second icon the background image still does not appear.
     
  5. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    I should have been clearer, the icons that I'm hovering over are world objects, not canvas objects.
    The code is referencing the canvas object infoBackground

    It seems that OnPointerEnter is purely for canvas objects and wouldn't meet my needs.
    Thank you for the reply, though.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Most likely you are either operating on another object, OR your code is immediately reversing what it does.

    Here's how to debug it and find out:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  7. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    Thank you,
    This script is on two icons that are placed in the world, not on canvas.
    They both have exactly the same code.
    As you can see, I have placed a debug statement just before the call the SetActive.

    What happens in game is that it works for one object, but not the other.

    This is regardless of which object I hover over first. It works on object 1 but does not work on object 2.
     
  8. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    I changed the debug statement to add the name of the object:
    Code (csharp):
    1.  
    2. Debug.Log("Over the icon for " + transform.name);
    3.  
    When I hover over each icon, the name is correct. It still doesn't work.

    When I hover over one icon, the canvas background appears (through SetActive()). When I hover over the other one, SetActive() does not appear.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    I don't see a debug statement printing the
    name
    of the thing(s) you're manipulating.

    That would seem to me to be an important piece of data for you to solve your problem, right?
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Review the logic you use to decide if you're over something.

    Also, I would avoid writing code like this:

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - Star Manta on the Unity3D forums
     
  11. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    Fixed it. I added a boolean called "inThis" to determine if I was still in the instance of the code attached to the object
    Code (csharp):
    1.  
    2.        Vector3 mPos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
    3.  
    4.         if ((mPos.x > transform.position.x - offset) && (mPos.y > transform.position.y - offset) && (mPos.x < transform.position.x + offset) && (mPos.y < transform.position.y + offset))
    5.         {
    6.             Debug.Log("Over the icon for " + transform.name);
    7.             inThis = true;
    8.             infoBackground.SetActive(true);
    9.             DisplayInfo();
    10.  
    11.         }
    12.         else
    13.         {
    14.             if(inThis)
    15.             {
    16.                 infoBackground.SetActive(false);
    17.                 inThis = false;
    18.             }
    19.  
    20.  
    21.         }
    22.  
     
    Kurt-Dekker likes this.
  12. mmancuso666

    mmancuso666

    Joined:
    May 23, 2018
    Posts:
    9
    Line 8 of the original post.

    It was changed to provide the name of object. I showed that in a later post.