Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fel3web · Feb 03, 2020 at 06:40 PM · variablevariablesmethodpublicmethods

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_ek98vnTRplGj8Q · Feb 03, 2020 at 07:00 PM 0
Share

Can you please paste the code that calls the SetCarryPotion function

avatar image Bunny83 · Feb 03, 2020 at 07:25 PM 0
Share

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.

avatar image logicandchaos · Feb 04, 2020 at 02:18 AM 0
Share

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.

avatar image fel3web logicandchaos · Feb 04, 2020 at 08:11 AM 0
Share

Hi! the log says nothing. there are no errors as such it is just not working. sorry for being unclear.

avatar image logicandchaos fel3web · Feb 04, 2020 at 03:56 PM 0
Share

Where are you calling your method? Can you show the code.

avatar image dalessan9 · Feb 04, 2020 at 11:57 AM 0
Share

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.

avatar image INvalidSauce · Feb 04, 2020 at 04:05 PM 0
Share

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".

avatar image Bunny83 INvalidSauce · Feb 05, 2020 at 05:53 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 05, 2020 at 05:48 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges