This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers
Related
This question already has answers here:
Unity singleton manager classes
(9 answers)
Closed 3 years ago.
I have a code like this:
public bool foodEaten=true;
I'm able to view it in the Unity Editor having a checkbox with a check.
If I refer the same variable on a different scene, it asks me to change it to static. If I do that, I'm not able to view it in the Unity Editor. How will I do both in which I can still see the same variable in Unity Editor, but I'm able to refer it on a different scene. Thanks!
There is one simpel solution, you can make a local non-static variable "foodEaten_nonstatic" and give it the same value as the static one.
public void Update(){
foodEaten_nonstatic = foodEaten;
}
But you can't change the value of it in the inspector!
This question already has answers here:
What's the difference between [Something] and [SomethingAttribute] [duplicate]
(3 answers)
Closed 7 years ago.
I hope this wasn't asked already. But i found nothing. If something exists, thanks for the note.
The title says it all i think.
I've seen these two variants. But in my opinion it does the same. And why can i use both. Thanks for education.
// variant 1
[ContentProperty("Text")]
// variant 2
[ContentPropertyAttribute("Text")]
You can omit the word "Attribute" when writing attributes over something. The actual class is called ContentPropertyAttribute. Both of your lines do exactly the same and use the exact same attribute class.
This question already has answers here:
Can you add to an enum type in run-time
(7 answers)
Closed 8 years ago.
I have this enum function with some elements:
public enum TrackingTypeEnum
{
None,
Start,
PageView,
Foreground,
Background,
Push,
}
And I want to add some elements there, but not manually in the function, i want to use "new" command but dosn't work.
I've tried this:
TrackingTypeEnum Custom = new TrackingTypeEnum;
Any solution?
Thanks!
According to official documentation, you can't use enum keywork in such a way. In sort, an enum is a list of constants you can assign values to.
Read: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
That's not what enums are for- you should only use them to store every possible state of an object.
If you really do want to use enums, you can store those additional values in a dictionary, like it's covered in this SO answer:
C#: can you add to an enum type in run-time
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the correct way to create a single instance application?
What is a good pattern for using a Global Mutex in C#?
Suppose i have created an exe i want that exe must run only once ..how it is possible please give suggestion
If I understand your problem correctly this has nothing to do with having a singleton implementation. You simply need to check if your executable is currently running.
You can do this by calling Process.GetProcesses() or Process.GetProcessesByName(NameOfExecutable) and checking the return values.
Alternatively use a Mutex as suggested above by others.