How to use value from a function in another function
Hi ,i need some help using same Variable in 2 differents function which are in the same script/class I need to use the value of the variable which is seted randomly in function A but when i use it in function B the value is equal to 0. My function are both void i can't make it Int because it is atached to a button. I have try different thing like use pointer but it dont work with unity i get some kind of error , can someone help me? ^^
Ps:Please dont send me link to a tutorial i already watch alot it will not help me,just tell if it is possible or not and if yes how? Sorry for my bad english #thankyou
Answer by Graphics_Dev · Mar 03, 2016 at 03:15 PM
Just declare your variables outside of the functions ;)
public class SomeClass : MonoBehaviour
{
string someStringValue = "state1";
void Update()
{
MethodNumber1();
}
void MethodNumber1()
{
if(someStringValue == "state1")
someStringValue = "state2";
MethodNumber2();
}
void MethodNumber2()
{
if (someStringValue == "state2")
someStringValue = "state1";
}
}
Thank you for your answer. I already do this, i always declare my varialbe outsite the function but the things was that when i use a variable in FunctionA from FunctionB in function A the variable was always equal to 0 and in FunctionB it always change ( i know it becaus i use Debug.Log($$anonymous$$yVariable) to see if it works ) :/ Or maybe i just did something wrong in my code but i agree with you @Graphics_Dev this should world but in my case it didn't Have a good day
This might be very easy to solve if we saw your code... what type is the variable you are trying to change? If its a value type and you pass it in as a function parameter, then change the parameter in the function, the original value doesn't change. There are mamy Q's and A's about that here
Your answer
Follow this Question
Related Questions
How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers
HELP! How to make Update function start after delay? C# 2 Answers
Having trouble deleting objects from a list after they reach a certain scale. 0 Answers
Problems with respawning using a very simple script 1 Answer
Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 1 Answer