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 Prmetheus · Apr 09, 2016 at 05:33 PM · c#scripting problemsprite

Can´t access sprite from another script [C#]

Hey! Im following a tutorial about an easy inventory system.

My problem: I try to access a sprite decleration in inventory.cs from slot.cs.

Heres the Inventory.cs Script (i comment the line where the issue appears:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class SlotNew : MonoBehaviour {
 
     private Stack<Item> items; 
     public Text stackText; 
 
     public Sprite slotEmpty;
     public Sprite slotHighlight;
 
 
     // Use this for initialization
     void Start () {
         items = new Stack<Item> ();
         RectTransform slotRect = GetComponent<RectTransform> ();
         RectTransform txtRect = GetComponent<RectTransform> ();
 
         int txtScaleFactor = (int)(slotRect.sizeDelta.x * 0.60);
         stackText.resizeTextMaxSize = txtScaleFactor;
         stackText.resizeTextMinSize = txtScaleFactor; 
 
         txtRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, slotRect.sizeDelta.y);
         txtRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, slotRect.sizeDelta.x);
 
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void AddItem(Item item)
 
     {
         items.Push (item);
 
         if (items.Count > 1) {
 
             stackText.text = items.Count.ToString ();
         }
 
         ChangeSprite (item.spriteNeutral , item.spriteHighlight);   **<-- spriteNeutral and spriteHighlight are RED.** 
     }
 
 
     private void ChangeSprite(Sprite neutral, Sprite highlight)
 
     {
         GetComponent<Image> ().sprite = neutral;
 
         SpriteState st = new SpriteState ();
 
         st.highlightedSprite = highlight;
         st.pressedSprite = neutral; 
 
         GetComponent<Button> ().spriteState = st; 
 
     }
 }


and here the slot.cs Script

 using UnityEngine;
 using System.Collections;
 
 public enum ItemType{MANA, HEALTH};
 
 public class ItemNew : MonoBehaviour {
 
     public ItemType type;
 
     public Sprite spriteNeutral;   **<-- i try to access this**
 
     public Sprite spriteHighlight; **<-- and this**
 
     public int maxSize;
 
 
     public void Use()
     {
         switch(type)
         {
         case ItemType.MANA:
             Debug.Log("I just used mana potion");
             break;
         case ItemType.HEALTH:
             Debug.Log("I just used health potion");
             break;
         
         }
     }
 
 
 
 }
 


i´ve tried almost everything possible and it seems no one else has the same problem i do in this tutorial. Link to the tutorial and the right part where he talks about this function: https://youtu.be/KLaGkc87dDQ?t=1h11m45s

Any ideas why i cant access it? Mono says: "Item" does not contain a definition for spriteNeutral.

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 gjf · Apr 09, 2016 at 05:22 PM

it looks like your problem is because you haven't defined Item. your definition for those variables is ItemNew. that's not the same ;)

and if that second script is in a file called slot.cs then the class name should be something like Slot or slot...

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 Prmetheus · Apr 09, 2016 at 06:22 PM 0
Share

The script is the same like in the class, i only called it slot for a better understanding.

I´ve changed it now to Item and it works. Can you tell me the line where he defined Item? I just ask, because changing it to ItemNew (my actuall script name) still gave me errors, so i just trying to understand where he defines it exactly.

thanks already! :)

avatar image gjf Prmetheus · Apr 09, 2016 at 08:33 PM 0
Share

without seeing the tutorial that you used, it's impossible to tell - i don't have time to watch a tutorial video...

it's good practice to have class names the same as script filenames.

also, these 2 lines:

 RectTransform slotRect = GetComponent<RectTransform> ();
 RectTransform txtRect = GetComponent<RectTransform> ();

will get the same value - is that intended?

avatar image Prmetheus · Apr 09, 2016 at 08:35 PM 0
Share

Oh yeah, i didn´t except you to watch the tutorial at all, i thought you can see that already in my posted scripts here.

Thanks anyway! :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sprite Shadow Script Problem 2 Answers

How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer

How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer

Sprite image not changing 0 Answers

Multiple Cars not working 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