- Home /
The question is answered, right answer was accepted
Weird Variable Scrope Problem
So basically in start, I am assigning DayNightController
void Start()
{
DayNightController dayNightScript = DayNightCycle.GetComponent<DayNightController>();
}
Then, when i try to access it anywhere else.
dayNightScript.daySpeedMultiplier = 0.05f;
It doesn't recognize dayNightScript, doesnt matter where i put it, not sure why it does this.... (Its probably something simple, just cant come up with it XD)
Answer by saschandroid · Nov 03, 2016 at 10:10 AM
"Then, when i try to access it anywhere else." Do you mean trying to access it outside Start()
? That's not possible, it's only known in that method. You have to declare it outside:
public class YourClass : MonoBehaviour
{
DayNightController dayNightScript;
void Start()
{
dayNightScript = DayNightCycle.GetComponent<DayNightController>();
}
}
Nop, as in i am trying to acces it a if statement in the start.
We need to see more of your script, not just the declaring and assignment. Can you post the entire Start function/or try to cut it down to the important parts? Also what is the error message? Is it a NullReferenceException during runtime or are you getting a compiler error?
Nothing of that, it simply way doesn't recognize the variable in Visual Studio already.
GameObject Declaration:
public GameObject DayNightCycle;
Start Function
void Start()
public virtual void OnGripped(ClickedEventArgs e)
{
if (Gripped != null)
Gripped(this, e);
DayNightController DayNightScript = DayNightCycle.GetComponent<DayNightController>();
DayNightScript.daySpeed$$anonymous$$ultiplier = 0.5f;
}
public virtual void OnUngripped(ClickedEventArgs e)
{
if (Ungripped != null)
Ungripped(this, e);
DayNightController DayNightScript = DayNightCycle.GetComponent<DayNightController>();
DayNightScript.daySpeed$$anonymous$$ultiplier = 0.05f;
}
Small Edit: Right now i am declaring it in each function, this way it works.
Well, the first part i am not 100% sure, it's a script from s$$anonymous$$mVR and it seems to be working so yeah. Anyways, just to be sure i tested it on another script, and it seems to work there, so yeah i have to figure out how to do it then, considering its a API. Anyways, thanks for the help!
Follow this Question
Related Questions
Accessing variable from another class gives initial value 1 Answer
Accessing EditorWindow variables from game code (only on editor play) is not possible? 1 Answer
How to approach individual variables for cooking game 1 Answer
Get A Variable from another Script in Unity iPhone 2 Answers
Can I declare a variable inside a function which has instance scope? 1 Answer