Search Unity

Canvas Groups not Independent from each other?

Discussion in 'Scripting' started by Clix4k, Aug 20, 2017.

  1. Clix4k

    Clix4k

    Joined:
    Oct 10, 2016
    Posts:
    1
    I have player interact script for picking up and dropping stuff. I wanted to add a hover "Press E" whatever too let the players know what they can an cannot pick up. Didn't want the basic button style bc it looks bad. Long story short I figured out that I could use Canvas Groups to fade in and out with the alpha. The only problem is (even when I am calling different Canvas Groups) all canvas groups will fade in and out.


    Before I hover over, only the canvas in the said hitting parent should enable:




    After Raycast hits... both canvas groups alpha to 1.




    This is the anvil script setting:

    This is the cube you see on the left settings:


    This is what goes on the item I want to grab. really just wanna know why it is calling all Canvas Groups and not the one I set.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Hover : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.     public CanvasGroup canvas;
    9.     public float speed;
    10.     public bool holdingCanvas;
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (player.GetComponent<Holdable> ().hit.collider == null) {
    16.             leaveTip ();
    17.         }
    18.  
    19.         if (player.GetComponent<Holdable> ().holding != false) {
    20.             if (player.GetComponent<Holdable> ().hit.collider.tag != "item") {
    21.                 leaveTip ();
    22.             }
    23.         }
    24.  
    25.         if (player.GetComponent<Holdable> ().holding == true) {
    26.             if (player.GetComponent<Holdable> ().hit.collider.tag == "item" && player.GetComponent<Holdable> ().hit.collider != null) {
    27.                 leaveTip ();
    28.             }
    29.         }
    30.  
    31.         if (player.GetComponent<Holdable> ().Hitt == true) {
    32.             if (holdingCanvas == false) {
    33.                 if (player.GetComponent<Holdable>().hit.collider.tag == "item") {
    34.                     speed = 5 * Time.deltaTime;
    35.                     canvas.alpha += (speed);
    36.                 }
    37.             }
    38.         }
    39.  
    40.  
    41.     }
    42.     void leaveTip(){
    43.         speed = 5 * Time.deltaTime;
    44.         canvas.alpha -= (speed);
    45.         holdingCanvas = true;
    46.     }
    47. }
    48.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Right now, unless I'm missing it, you aren't determining what target is hit. Update runs constantly on every script. So, both objects are checking there update. As soon as your if statements become true, they both trigger leaveTip. You aren't determining what target should run leaveTip.

    Think of it this way. If there are two people in a room and they are told to stand up when a sound is played, they will both stand up when it's played. However, if you change it and give them headphones and ask them to stand only if they hear the sound through their headphones, that person will stand, leaving the other sitting. I'm not sure if this helps, but hopefully!