- Home /
Variable Not Changing In Method
So i assign this variable:
public string PotionCarry = "None";
And use it like this in a method here:
public void SetCarryPotion(string Potion)
{
if(PotionCarry == "None")
{
PotionCarry = Potion;
print("Transfer");
}
print(PotionCarry);
}
This method is in the same script, and i have checked that when triggered "Transfer" is printed to the console. These are the only mentions of PotionCarry, so what is happening and how can i fix it? Thanks!
Edit: looked through my code in the trigger function calling the setcarrypotion() and it's because i set the variable (which then would be set as PotionCarry) to none at the start of the function. facepalm
Sorry for being stupid and wasting your time,
Random noob
Can you please paste the code that calls the SetCarryPotion function
Uhm I think we are missing something here. What are your actual observations? What log messages do you actually get when you do what? You probably call "SetCarryPotion" from somewhere. What value do you pass as parameter? What's the result of your logs?
$$anonymous$$eep in $$anonymous$$d that your SetCarryPotion can only ever change PotionCarry when it is "None". Once you set it to any other value you can never change it through this method since you will never enter the if statement where you actually change the variable.
Well I'm not entirely sure what you are trying to accomplish, if you showed a pic of the log it would be much more informative. What do you mean by what is happening and how do I fix it? If you mean why is it only printing transfer and setting the variable once, that is because that will only happen if the variable equals "None". As a side note you should also use String.Compare to compare 2 strings.
Hi! the log says nothing. there are no errors as such it is just not working. sorry for being unclear.
Where are you calling your method? Can you show the code.
Why did you print an unrelated string, when you could have printed Potion?? That would give further information on what string the SetCarryPotion method is trying to set the variable PotionCarry to be.
I don't quite understand the problem? Is it that when you run the method using some string as the input "Potion", the "print" command doesn't print anything or does it just print the same thing as before the method ran? The way you have it setup, it will ONLY ever change string "PotionCarry" if you give it an input string = "None".
No, you read the code wrong. PotionCarry can only be changed by this method when "PotionCarry" is "None". So once you called this method with any value different from "None", PotionCarry would be set to that value and can never be changed again through this method. It's like a one-time set method. I also don't really get the point of this method.
Answer by kskjadav007 · Feb 05, 2020 at 12:44 PM
Do This.
public string PotionCarry = "None";
private void Start()
{
SetCarryPotion("NewPotionCarry");
}
public void SetCarryPotion(string Potion)
{
if (PotionCarry == "None")
{
PotionCarry = Potion;
print("Transfer");
}
print(PotionCarry);
}
And what would be the point of all this? You could simple replace all your code with
public string PotionCarry = "NewPotionCarry";
And you get a similar behaviour. Once PotionCarry is something different than "None" the method SetCarryPotion can not change the variable anymore, no matter what value you pass in.
Your answer
Follow this Question
Related Questions
cant change variable from a method? timer stays at 0! 1 Answer
Calling a variable based on a variables name? 1 Answer
Accessing variable from a method in another script and gameObject 2 Answers
Global Variables Refuse to Cooperate 1 Answer
Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer