- Home /
Other
Enums and Class - A general question
Hi,
Am I right in thinking that when I create and Enum or a Class that it is automatically visible throughout the project. If I understand this right, could I use these to create and update my data structure more easily. I think I have been doing things the hard way face palm
If the above is correct, is there anything else that behaves in this manner? And are there any pitfalls I should watch out for using these methods?
Thanks
Paul
If you create your enums or classes public in the global namespace they will be visible in the project. But I'm confused, because every script file you've written in Unity has been a class... If you want help in maintainable data structures, I'll gladly throw in my 2 cents. First I'll need to know what it's for and preferably what you have now.
I'm not sure what behavior you're referring to, static classes?
Hi Jamora,
Unity is the first time that I have actually really started to learn coding, and so I have been learning bit by bit in my spare time, but it looks like I might have to learn the basics about coding in general. As your statement everything I have written is technically a class which raised my eyebrows lol!
So I have started exploring classes to hold information about weapons. For example (I'm using UnityScript):
class WeaponsClass {
var weaponName = "";
var weaponIs$$anonymous$$issile : boolean;
var weaponIsLaser : boolean;
var weaponIsProjectile : boolean;
var weaponPowerUsage : float;
//And so on...
}
I'm also exploring enums to select what weapon to use. I've used them before but not much. So last night I noticed that I could not use the same name for a class or an enum as I was about to re-create them in another script. That's when I realised that they seem to be public in the entire project. It was then I realised I could be a lot more efficient in the way I structure everything.
So that's the background to it all. I'm really excited that I could be more efficient about sharing data in the project without using static variables and so on. So it got me wondering if anything else like class, and what I should be careful about.
If also noticed untiy have updated the tutorials since the last time I looked and found this one: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes
I'll be looking through more of them today.
I think you the Unity lessons will answer most of your questions.
You asked for other things like classes, so I would like to point out that there is also a data type similar to a class called a struct. It is a value type ins$$anonymous$$d of a refrence type.
As for the pitfalls you asked, classes are stored by refrence, so you might have two different fields point to the same instance of a class, in which case any change made to one of them will be reflected in the other. Structs, being value types, don't exhibit this behavior.
Yeah, spent a while on them today and now re-writing a bunch of stuff. Feels good! Thanks Jamora.
Follow this Question
Related Questions
Enum style variable 2 Answers
Finding the length of an enum in a different class 1 Answer
Unknown identifier: 'types'. js(38,61) 1 Answer
Logic and datatype existential problem 2 Answers