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 slipperydemon · Jan 10, 2018 at 03:38 AM · variablereference

variable returns from referenced script as 0

I try to reference a variable in another script and it comes back as 0, no idea why, here's my code

 void OnTriggerEnter2D(Collider2D other)
 {
         if(other.gameObject.tag == "candy")
         {
             candyKainz++;
             other.gameObject.GetComponent<Candycaine>().collected();
         }
 }
 
 public class Counter : MonoBehaviour {
     public Text numeber;
     public Characterbehavior h;
    
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {      
         numeber.text = h.candyKainz + "/100";      
     }   
 }
Comment
Add comment · Show 5
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 KittenSnipes · Jan 10, 2018 at 09:23 AM 0
Share

@slipperydemon

What does the collected() function do? It would be a lot more clear if we had the actual class that does the math.

avatar image Legend_Bacon · Jan 10, 2018 at 09:40 AM 0
Share

As far as I can tell, you're never assigning "h" in your code above. So I'm assu$$anonymous$$g that "h.candy$$anonymous$$ainz" would return 0 by default.

If "Characterbehavior" is on the same object as "Counter", then you may want to do h = GetComponent<Characterbehavior>() on Start().

avatar image KittenSnipes Legend_Bacon · Jan 10, 2018 at 09:42 AM 0
Share

Wow I couldnt really see that. His code is a bit difficult to read. I think that is definitely a huge problem. He should set h's candy$$anonymous$$ains in the OnTriggerEnter2D function maybe

avatar image Legend_Bacon KittenSnipes · Jan 10, 2018 at 09:45 AM 0
Share

They may have several classes on the same script, which can be okay in some cases. In the OnTriggerEnter2D function, they only increase an internal number (some kind of score I'm assu$$anonymous$$g), and tell the pickup item to set itself to "collected", which might mean playing a sound, particle, and despawning. So the code isn't actually wrong, it's just formatted a little weird in their original post.

Cheers,

~LegendBacon

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sisse008 · Jan 10, 2018 at 12:23 PM

you are not referencing a variable anywhere in the code you posted. you are referencing a Candycaine component with the line:

 other.gameObject.GetComponent<Candycaine>().collected();

and calling a function (collected()) in the Candycaine component.

Comment
Add comment · Show 1 · 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 sisse008 · Jan 10, 2018 at 12:25 PM 0
Share

for us to help you, please post both scripts and mention which variable you are referring to.

avatar image
0

Answer by creganjordan292 · Jan 10, 2018 at 12:55 PM

Your "Characterbehavior" needs to be assigned. If it extends a MonoBehaviour, you can add this through the inspector or calling a "GetComponent" on a GameObject which does have one. Other wise you use: h = new Characterbehavior() to create a new instance and assign candyKainz before you try to read the variable.

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

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

81 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

Related Questions

Accessing Variables (again) 2 Answers

Reference variable 2 Answers

GUI.Style referencing, one script to another 1 Answer

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

My GameObject refrence in my script keeps resetting if i die 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