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 /
avatar image
0
Question by Mike_El · May 26, 2018 at 11:13 AM · spriteimagegetcomponent

Trying to call Image varible with GetComponent, doesn't want to work

Basically this is the way my script is set up atm

 Storage str;
 Dig dig;
 Image img;
 
 public GameObject storage1, storage2;
 
 void Start (){
     str = FindObjectOfType<Storage> (); //Another Script
     dig = FindObjectOfType<Dig> (); //Another Script
 
     img = GetComponent<Image> ();
 }
 
 void Update () {
 }
 
 public void UpdateInventory(){
         if (dig.temp == "Object") {
             if (storage1.GetComponent<Image> ().sprite == str.spr1) {
                 storage1.GetComponent<Image> ().sprite = str.spr2;
                 return;
             } else if (storage2.GetComponent<Image> ().sprite == str.spr1) {
                 storage2.GetComponent<Image> ().sprite = str.spr2;
                 return;
             }
         }
     }
 }

I need to be able to find a way to use the variable img instead of always having to write out GetComponent<Image> ().sprite all the time because it will get tedious and repetitive.

I've checked out the documentation and other questions and stuff but still can't figure this one out. I've tried out something like storage1.img.sprite = but the error that comes up is something like img isn't defined or something.

Any help would be appreciated.

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 Chik3r · May 26, 2018 at 01:14 PM

Because storage1 & storage2 are a GameObject, the only way to get an Image component would be to use GetComponent<Image>(), GameObject has no variable called img-image-etc. that has a reference to an Image component.
What you could do is add a script to your storages and have a reference to that script, and inside that script have a variable called "img" of type Image that gets defined at the start.

Comment
Add comment · Show 2 · 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 Mike_El · May 26, 2018 at 01:36 PM 0
Share

Storage str is a link to a script within storage1 and 2. So I just define the GetComponent within an Image variable in Storage then I just call str.img within the if statements?

avatar image Mike_El · May 26, 2018 at 01:54 PM 0
Share

Okay, I think I've got this figured out. When I use the direct script (Storage) I need to hold the variable inside of a sprite for it to not throw out any errors. So even though it is technically an Image ins$$anonymous$$d of SpriteRenderer, I still need to use a Sprite variable to transfer the data across.

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

114 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

Related Questions

How can I put the single pieces of a sliced Sprite into an array? 0 Answers

Changing a UI image sprite in scripts results in a black texture 0 Answers

Unable to drag image from Assets folder to Sprite: None under Sprite Renderer. 0 Answers

How do I change the import settings when using sprite.create with a WWW.texture from a png image. 1 Answer

How do i generate random types of sprites in game ?,Can i set a variable to an image asset ? 0 Answers


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