Cannot hide a canvas from trigger (err:Cannot implicitly convert type `UnityEngine.Canvas' to `UnityEngine.GameObject')
Hello i am still a begginer in scripting and i am looking trought some tutorial to make some test. i am actually working on a script that hide a canvas when the player is on a zone or not (a light bulb that is Lighten when the player is in the light or not)
Its made from 2 script (one on the player itself and one on every light source with a sphere collider set to trigger)
LightCanvas is parented to another canvas.
Here's the one on the player
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class InLight : MonoBehaviour {
public bool OnOff;
private GameObject lightUI;
void Start ()
{
SetInitialReferences ();
}
public void Inlight()
{
if(OnOff = true)
{
lightUI.SetActive(true);
Debug.Log ("On");
}
}
public void Outlight()
{
if(OnOff = false)
{
lightUI.SetActive(false);
Debug.Log ("Off");
}
}
void SetInitialReferences()
{
lightUI = GameObject.Find("LightCanvas").GetComponent<Canvas>();
}
}
and here's the one on every light source:
using UnityEngine;
using System.Collections;
public class LightZone : MonoBehaviour
{
private InLight illumination;
void Start ()
{
SetInitialReferences();
}
void OnTriggerEnter (Collider other)
{
illumination.Inlight ();
//Debug.Log (other.name+" is in the light");
}
void OnTriggerExit (Collider other)
{
illumination.Outlight ();
//Debug.Log (other.name+" is in the Dark");
}
void SetInitialReferences()
{
illumination = GameObject.Find("Player1").GetComponent<InLight>();
}
}
I get that error in the console: Assets/MyAsset/Scripts/InLight.cs(35,17): error CS0029: Cannot implicitly convert type UnityEngine.Canvas' to
UnityEngine.GameObject'
Answer by jeffbori · Oct 05, 2016 at 05:49 AM
Script on the player:
(plug the image in the required field in unity inspector)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class InLight : MonoBehaviour
{
public bool OnOff;
public Image lightUI;
public void Offlight ()
{
OnOff = false;
lightUI.enabled = false;
Debug.Log ("OFF");
}
public void Onlight ()
{
OnOff = true;
lightUI.enabled = true;
Debug.Log ("ON");
}
}
Script on every light source:
(plug the player1 in the required field in unity inspector)
using UnityEngine;
using System.Collections;
public class LightZone : MonoBehaviour
{
public InLight illumination;
void OnTriggerEnter (Collider other)
{
illumination.Onlight ();
Debug.Log (other.name+" is in the light");
}
void OnTriggerExit (Collider other)
{
illumination.Offlight ();
Debug.Log (other.name+" is in the Dark");
}
}
Answer by seckincengiz · Oct 04, 2016 at 09:52 PM
private GameObject lightUI;
void SetInitialReferences()
{
lightUI = GameObject.Find("LightCanvas").GetComponent<Canvas>();;
}
light UI -> GameObject and Canvas -> Component. Here you are trying to call component as a GameObject. If you want to use component change lightUI as a Component variable. By the way your approach is not the best practice here. You can use another approach without using Find() method too much. It is not good for performance.
You can't use SetActive() method for a component. If you want to enable/disable a component you should use myComponent.enabled = true in your case :
lightUI = GameObject.Find("LightCanvas").GetComponent<Canvas>();
lightUI.enabled = true
And please comment here, don't post your comment as an answer.
Sorry it's the first question i am asking for the forum, i will be sure to not put it in answer again,
So here's the script now:
using UnityEngine;
using System.Collections;
public class InLight : $$anonymous$$onoBehaviour {
public bool OnOff;
public Component lightUI;
void Start ()
{
SetInitialReferences ();
}
public void Inlight()
{
if(OnOff = true)
{
lightUI.enabled = true;
Debug.Log ("On");
}
}
public void Outlight()
{
if(OnOff = false)
{
lightUI.enabled = false ;
Debug.Log ("off");
}
}
void SetInitialReferences()
{
lightUI = GameObject.Find("LightCanvas").GetComponent<Canvas>();
}
}
but i still got that error message:
Assets/$$anonymous$$yAsset/Scripts/InLight.cs(18,33): error CS1061: Type UnityEngine.Component' does not contain a definition for
enabled' and no extension method enabled' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
lightUI should be declared as Canvas
private Canvas lightUI;
No, it is not the case. Canvas can be used as a game object or a component. In your first code i assumed that you use it as a component. Check this answer : http://answers.unity3d.com/questions/794362/how-to-enable-and-disable-a-canvas-window-by-scrip.html
if you still have problem please share your hierarchy and i will try to help you more.
Ok i made some change to be a bit more organize, so want i want to do is access LightOnOff and Desactive it (option A) or change the alpha color (option B)
(HUD and LightCanvas are both canvas)
The Script is on Player1
I finally fixed it, it was a bit more simple than i was thinking and i did,nt had tu use Find.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094019im_/https://answers.unity.com/themes/thub/images/avi.jpg)