Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by digimbyte · Sep 15, 2013 at 12:03 AM · variable

Serialized variables not sharing

Hi, I have a script that has a lot of variables and I really wanted to group them and collapse them to help organize them.

so I did a bit of research and apparently I use this [System.Serializable] after moving those variables into that public class, its having trouble accessing those variables in my monoscript (the script cannot find them)

error CS0103: The name `Stat_healthCurrent' does not exist in the current context

for some reason its not passing it on, maybe I'm doing something wrong.

Comment
Add comment · Show 6
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 Benproductions1 · Sep 15, 2013 at 02:14 AM 0
Share

What line is the error on? Its better to post the code here ins$$anonymous$$d of some foreign website. Also it's better if you condense the code to the smallest replicatable solution. So that the error still occurs but you have very little code left to go though.

avatar image Benproductions1 · Sep 15, 2013 at 03:24 AM 0
Share

Please format your code. If you don't know how watch the tutorial video on the right

avatar image digimbyte · Sep 15, 2013 at 03:43 AM 0
Share

The problem is simple, the functions healTick, applyDamage cannot find the variables ever since i put them in a Class list at the top. for some reason they aren't passed on or accessible inside the script itself. maybe if you read what people say, you'll stop worrying about something redundant. the code isn't messy, apart from a few extra lines. help, or don't help.

It's not a format issue, understand? and its not a big script. do I have to hold your hand to find what the problem is? I already mentioned that I know what the problem is, but not how to fix it.

avatar image Benproductions1 · Sep 15, 2013 at 04:05 AM 0
Share

@digimbyte The script is now properly formatted. I was not referring to indentation/spacing etc, just he common formatting of questions on this website. $$anonymous$$aking a question readable is a key part in getting answers on these websites, so please adhere to the regulations from the FAQ. I am not being childish, I am merely moderating this question and frankly I could answer your question but with the response you just posted I feel like its not work my time. Without standards and people to enforce them (us high karma users/real mods) this website would be unusable. So next time someone asks you for a simple 20 second fix of your question, when you should know that it doesn't meet the standards, please fix it up without bothering them. We do this for free you know :)

avatar image digimbyte · Sep 15, 2013 at 06:06 AM 0
Share

Communication is important, I was looking at my code. Also posting huge sections of script is garbage. also if they wanted these 'standards' enforced they would create a more specialized system.

one thing you have to know, is a lot of people don't know how to properly submit questions, and its not enforced in a diplomatic manor. ins$$anonymous$$d you get up me about format when the only formatting I know is code wise. some consideration and time can go a long way. I maybe 'grumpy' but I'm not entirely in the wrong.

Show more comments

2 Replies

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

Answer by Benproductions1 · Sep 15, 2013 at 04:33 AM

Hello,

Even though you were being a d*** to me, I'm still going to answer your question:

Learn how classes work

You can't go:

 public class Vars {
     public int i = 5;
 }
 
 public class lol : MonoBehaviour {
     public Vars vars;
     
     public void Awake() {
         print(i);
     }
 }

What would i be? It's not defined anywhere in the lol class is it?
If you have something in a class you need to access it either through the class itself (static) or through an instance of the class, like so:

 public class lol : MonoBehaviour {
     public void Awake() {
         print(vars.i);
     }
 }

Next Time you ask a question, simply copy and paste the error message because just that would have come up with this answer in probably less than 10 seconds. This is the importance of asking questions properly :)

Hope this helps,
Benproductions1

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 digimbyte · Sep 15, 2013 at 05:09 AM 0
Share

I did mention what the question was, you went on about formatting. I didn't mean to be a D*** and for that I'm sorry, Thanks for seeing it through.

Thank you for explaining it for me, I was following an old tutorial which explained that the serialized must be before the monobehaviour

avatar image
0

Answer by Tarlius · Sep 15, 2013 at 02:20 AM

You need to use the [System.Serializable] tag for each class, not once at the top. Basically it means "do this for the next item".

...was my first reaction. However, your compiler problem is caused by you not updating the code to reference the new variables as members of the class you've made. All instances of Stat_healthCurrent need to read group2. Stat_healthCurrent now (except the one in the new class, obviously)

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 digimbyte · Sep 15, 2013 at 03:03 AM 0
Share

Well that's the problem, what's going on is that the script in lines 95-117 cannot find the variables.

where I process the health and armor, it simply does not find the variables in group2

avatar image digimbyte · Sep 15, 2013 at 04:22 AM 0
Share

do know how to fix the variables that aren't being detected?

the script works perfectly fine when the variables aren't in that class, when i moved them there, it seems the variables aren't detected or passed into the main script (after monobehaviour)

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Learning C# instead of doing what tutorials say... 2 Answers

How does this code generate movement using player input? Does it have some in-engine code? 1 Answer

How to expose variables in c# according to selected booleans 1 Answer

How to write "if (var int = var int 2)" ? 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