Search Unity

[SOLVED] Canvas group Alpha does not change to 1

Discussion in 'UGUI & TextMesh Pro' started by Inferi, Oct 17, 2018.

  1. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    Hi

    I have a door and when a player gets inside the trigger of that door the canvas group alpha will be set to 1 and when the player is outside this trigger, the canvas group alpha will be set to 0.

    All this works great, - on one of the doors "The first in the tree view"!

    Every other door "no matter if it's a copy or a prefab of the first one", does not set canvas group alpha to 1 "the same code is used".

    I use a canvas in the root and the canvas is found in all doors.
    I have checked so every trigger is in the right place.
    All doors work as they should otherwise.

    Do anyone have any idea why this is happening!?


    Code for the door "sorry for lack of comments, will be putting them in later :)":

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ApartmentBuilding01_Door_Open : MonoBehaviour {
    7.     private GameObject InteractionCanvas;
    8.     private Text InstructionText;
    9.     private Animator _animator;
    10.     private bool DoorIsOpen = true;
    11.     private bool playerIsWithinCollider = false;
    12.     public bool DoorIsActive = false;
    13.     private string InteractionInstructions;
    14.     private bool PressedKey = false;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         _animator = this.GetComponentInParent<Animator>();
    19.         InteractionCanvas = GameObject.Find("Canvas_Interaction");
    20.         InstructionText = InteractionCanvas.transform.Find("Background/InteractionInstructions").GetComponent<Text>();
    21.     }
    22.  
    23.     private void OnTriggerStay(Collider other) {
    24.         if (other.tag == "Player") {
    25.             playerIsWithinCollider = true;
    26.         }
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.         if (playerIsWithinCollider) {
    32.             if (Input.GetKeyUp(gameControl.theInteractionKey) && PressedKey == false) {
    33.                 PressedKey = true;
    34.                 DoorIsOpen = !DoorIsOpen;
    35.             }
    36.             if (PressedKey == false) {
    37.                 InteractionInstructions = "Press [" + gameControl.theInteractionKey + "] to use door";
    38.             }
    39.             if (Input.GetKeyUp(gameControl.theInteractionKey) && PressedKey) {
    40.                 if (DoorIsActive == false) {
    41.                     InteractionInstructions = "Door can't be opened";
    42.                 }
    43.                 if (DoorIsActive == true) {
    44.                     HandleDoor();
    45.                 }
    46.             }
    47.             InteractionCanvas.GetComponent<CanvasGroup>().alpha = 1;
    48.             InstructionText.text = InteractionInstructions;
    49.         } else {
    50.             PressedKey = false;
    51.             InteractionCanvas.GetComponent<CanvasGroup>().alpha = 0;
    52.         }
    53.  
    54.     }
    55.  
    56.     void HandleDoor() {
    57.         if (DoorIsOpen) {
    58.             _animator.SetBool("close", true);
    59.             _animator.SetBool("open", false);
    60.         } else {
    61.             _animator.SetBool("open", true);
    62.             _animator.SetBool("close", false);
    63.         }
    64.     }
    65.  
    66.     private void OnTriggerExit(Collider other) {
    67.         InteractionCanvas.GetComponent<CanvasGroup>().alpha = 0;
    68.         PressedKey = false;
    69.         playerIsWithinCollider = false;
    70.     }
    71.  
    72.  
    73. }
    74.  
     
    Last edited: Oct 17, 2018
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    InteractionCanvas = GameObject.Find("Canvas_Interaction");


    That's going to mean that every copy of this script is referring to the same InteractionCanvas--the first GameObject with that name in whatever order Unity uses when it searches your whole scene.

    If the CanvasGroup you want it to reference is a part of the prefab that you're copying, you should use a public CanvasGroup variable and set its value in the inspector.

    If you need to link up to a CanvasGroup that's not part of the same prefab, you probably want to use something like GetComponentInParent<CanvasGroup>()
     
  3. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    Yes. I am using the same canvas for all. I only want the canvas that I have placed in the root. And then every door will change the canvas group Alpha on that one. It works Only for one door "the first in the tree view that is enabled" All other do not change the value but if I get the value from the canvas group alpha, it say the right thing from every door.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Oh. Then your problem is that every door is trying to change the value every frame, which means whichever one of them changes it last "wins".

    Example:

    You're currently inside door 2.
    1. You are not inside door 1, so door 1 sets alpha to 0
    2. You are inside door 2, so door 2 sets alpha to 1
    alpha = 1, because that is the last value assigned

    Now you're inside door 1.
    1. You are inside door 1, so door 1 sets alpha to 1
    2. You are not inside door 2, so door 2 sets alpha to 0
    alpha = 0, because that is the last value assigned

    Instead of having every door set the alpha, you want to have a single script somewhere that iterates over all of the doors, checking whether the player is inside any of them. As soon as it finds any door with the player inside, it sets alpha to 1, and then stops. If it gets to the end of the loop without finding any door the player is inside, only then does it set alpha to 0.
     
    Inferi likes this.
  5. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145

    That sounds like that could be the problem. I will see if I can fix and get back with the results :) Thanks
     
  6. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    Fixed it, so simple as well :)

    New ugly code "Only changed a little bit" :) :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ApartmentBuilding01_Door_Open : MonoBehaviour {
    7.     private CheckIfInsideDoorTrigger CIIDT;
    8.     private GameObject InteractionCanvas;
    9.     private Text InstructionText;
    10.     private Animator _animator;
    11.     private bool DoorIsOpen = true;
    12.     private bool playerIsWithinCollider = false;
    13.     public bool DoorIsActive = false;
    14.     private string InteractionInstructions;
    15.     private bool PressedKey = false;
    16.  
    17.     // Use this for initialization
    18.     void Start() {
    19.         CIIDT = GameObject.Find("Canvas_Interaction").GetComponent<CheckIfInsideDoorTrigger>();
    20.         _animator = this.GetComponentInParent<Animator>();
    21.         InteractionCanvas = GameObject.Find("Canvas_Interaction");
    22.         InstructionText = InteractionCanvas.transform.Find("Background/InteractionInstructions").GetComponent<Text>();
    23.     }
    24.  
    25.     private void OnTriggerStay(Collider other) {
    26.         if (other.tag == "Player") {
    27.             playerIsWithinCollider = true;
    28.             CIIDT.DoorTriggerActive();
    29.         }
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update() {
    34.         if (playerIsWithinCollider) {
    35.             if (Input.GetKeyUp(gameControl.theInteractionKey) && PressedKey == false) {
    36.                 PressedKey = true;
    37.                 DoorIsOpen = !DoorIsOpen;
    38.             }
    39.             if (PressedKey == false) {
    40.                 InteractionInstructions = "Press [" + gameControl.theInteractionKey + "] to use door";
    41.             }
    42.             if (Input.GetKeyUp(gameControl.theInteractionKey) && PressedKey) {
    43.                 if (DoorIsActive == false) {
    44.                     InteractionInstructions = "Door can't be opened";
    45.                 }
    46.                 if (DoorIsActive == true) {
    47.                     HandleDoor();
    48.                 }
    49.             }
    50.             InstructionText.text = InteractionInstructions;
    51.         } else {
    52.             PressedKey = false;
    53.         }
    54.  
    55.     }
    56.  
    57.     void HandleDoor() {
    58.         if (DoorIsOpen) {
    59.             _animator.SetBool("close", true);
    60.             _animator.SetBool("open", false);
    61.         } else {
    62.             _animator.SetBool("open", true);
    63.             _animator.SetBool("close", false);
    64.         }
    65.     }
    66.  
    67.     private void OnTriggerExit(Collider other) {
    68.         InteractionCanvas.GetComponent<CanvasGroup>().alpha = 0;
    69.         PressedKey = false;
    70.         playerIsWithinCollider = false;
    71.     }
    72.  
    73.  
    74. }
    75.  
    76.  
    And in a new script, placed on the canvas:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CheckIfInsideDoorTrigger : MonoBehaviour {
    6.     public Canvas canvas;
    7.     public void DoorTriggerActive() {
    8.         canvas.GetComponent<CanvasGroup>().alpha = 1.0f;
    9.     }
    10.  
    11. }
    12.  

    Thanks for the help. :)