- Home /
Store Multiple values in one variable
Is there a way to store multiple values in one variable? For example, there is a variable named sample. And var sample equals to an int of 5 and also an Boolean of true.
i.e. var sample = new (int 5, bool true); or something like this...
I feel like there is some sort of way of doing this since vector 3 can hold multiple values(x y and z).
I thought that an array might work but this variable has to go into an array. What I mean by this is: anArray.add(sample);
So is there anyway of doing this? thanks!
Answer by Kiwasi · Jul 03, 2014 at 04:00 AM
Two ways to do this, struct or a class. The key difference is that struct is passed by value and class is passed by reference.
Thank you for a quick reply!!
However, I am quite inexperienced in coding and also a newbie at C#. Would you be able to write a short example code declaring the struct or classes?
Although I spent a while reading the documentation you have shown me, I still quite don't understand how both of them work. (Especially classes)
Thank you again!
[EDIT] Also, would I be able to push this into an Array or a List?
Never$$anonymous$$d, I figured it out!!! I feel so smart haha :p Anyways thanks for your help!! Structs and classes are very useful. I believe that there is none of these in javascript is there?
JavaScript can do most things that C# can do. Classes and structs are kind of fundamental to OOP, so it should be able to do it.
However as a free piece of advice I would say learn C# ins$$anonymous$$d of JavaScript. There are a bunch of different reasons. Ultimately C# is more powerful, less error prone and exists outside of Unity.
Answer by bhartu · Jul 03, 2014 at 06:14 AM
you can use Dictionary to store two values.
like,
private Dictionary<int, bool> myDisctionary = new Dictionary<int, bool>;
myDisctionary.Add(12, false);
Debug.log(myDisctionary[12]); // will print false
Your answer
Follow this Question
Related Questions
Static Variable Problem 1 Answer
Sharing booleans between 2 scripts 1 Answer
Pass a function(float) as variable 2 Answers