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 easygaming · Jan 05, 2016 at 03:36 AM · scripting problemgraphics

Canvas has no Image when I call GetComponent()

alt textI added an Image to the Canvas but my Debug.Log() displays 0 Length when I call canvas.GetComponent().

         GameObject canvas = GameObject.Find("Canvas");
         crosshair = canvas.GetComponents<Image>();

         Debug.Log("Canvas null? " + (canvas == null).ToString());
         Debug.Log("CrossHair " + crosshair.Length);

Canvas is not null, but it does not have any Image in it.alt text

In the design view of Unity, I can clearly see the Image which is a child of the Canvas.

Did I do something wrong here? Any help will be appreciated, thanks.

screenshot.png (4.0 kB)
screenshot.jpg (79.2 kB)
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

1 Reply

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

Answer by incorrect · Jan 05, 2016 at 03:38 AM

Yes, you are doing wrong. Your Image component is on Image object, not on Canvas. Do it this way:

  GameObject canvas = GameObject.Find("Image");
Comment
Add comment · Show 3 · 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 easygaming · Jan 05, 2016 at 04:16 AM 0
Share

Thanks for your answer. But I encounter the error saying 'canvas is null'.

I tried the code below ins$$anonymous$$d, it worked.

     GameObject canvas = GameObject.Find("Canvas");
     Image crosshair2 = canvas.GetComponentInChildren<Image>();

     Debug.Log("Cams " + Camera.allCamerasCount);
     Debug.Log("Canvas null? " + (canvas == null).ToString());
     Debug.Log("CrossHair " + crosshair2.name);

I am not sure the difference between GetComponent() and GetComponentInChildren().

avatar image incorrect easygaming · Jan 05, 2016 at 04:23 AM 1
Share

Ok... Once again.

You have game object named Canvas that has Canvas component on it. Canvas component provides drawing of user interface.

You also have game object named Image with Image component attached to it.

So when you try to call GetComponent() on game object 'Canvas' you get nothing. You should use it on object named Image.

GetComponentInChildren() is a different method: it looks for a component on the object itself and on it's children, so in your case it finds component Image on one of children: on game object called 'Image'.

Please, take a look in Learn section and try develop your skills in Unity and general program$$anonymous$$g. Good luck!

avatar image easygaming incorrect · Jan 05, 2016 at 08:34 AM 0
Share

Thanks a lot for describing the difference.

I will try to learn more at the given link.

Now I am finding my Image and other GameObject in Start() function like this: GameObject img = GameObject.Find("$$anonymous$$yImage");

Thanks.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Gaia, MFPS, Enviro, WebGL Build only on browser with Enviro enabled is turning camera black with acid trip colors 0 Answers

Roll a ball and quiz (Pause scene, load scene and resume) 0 Answers

Fading in Smoothly between Sprites 0 Answers

Slider graphic refuses to update with value 1 Answer

Please Help! Scene Lighting has Bugged Out and Colors are Not Showing Correctly Anymore 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