Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jeffbori · Oct 04, 2016 at 09:31 PM · uitriggercanvas

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'

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
     }
 
 }
 


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image seckincengiz · Oct 04, 2016 at 10:29 PM 0
Share

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.

avatar image jeffbori · Oct 04, 2016 at 10:59 PM 0
Share

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

avatar image seckincengiz jeffbori · Oct 04, 2016 at 11:23 PM 0
Share

lightUI should be declared as Canvas

 private Canvas lightUI;
avatar image jeffbori · Oct 04, 2016 at 11:23 PM 0
Share

I acttuali have the 5.0 version, did i need the 5.4?

avatar image seckincengiz jeffbori · Oct 04, 2016 at 11:55 PM 0
Share

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.

avatar image jeffbori seckincengiz · Oct 05, 2016 at 02:00 AM 0
Share

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

alt text

exemple.jpg (158.2 kB)
avatar image jeffbori jeffbori · Oct 05, 2016 at 05:44 AM 0
Share

I finally fixed it, it was a bit more simple than i was thinking and i did,nt had tu use Find.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I check if there is no free space in the Text component? 1 Answer

How do i turn on/off background music and keep the changes when reload the scene 1 Answer

Screen space overlay canvas problems. 1 Answer

How to match position with Mini Map UI 0 Answers

How would I display text on an in game screen? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges