- Home /
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.
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.
Please format your code. If you don't know how watch the tutorial video on the right
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.
@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 :)
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.
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
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
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)
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
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)