Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How i can get variable from other script

Discussion in '2D' started by Loud_Lasagna, Nov 24, 2019.

  1. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    My game has changeable difficulty in the main menu and I'm wondering how I can access it from other scripts to change things like enemy damage and something like that. I've been trying to find something about it and so far, I don't quite understand how it works. Variable that i need access to is called mainMenuDifficulty.
     
    Last edited: Nov 24, 2019
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You would want to make static variables.

    Code (CSharp):
    1. public class OptionsMenu : Monobehavior
    2. {
    3. public static int difficulty;
    4. //Example: 1=Easy, 2=Normal, 3=Hard, etc...
    5. }
    6.  
    7. // On your other script, to access this variable
    8. public class ChangeOptions : Monobehavior
    9. {
    10. // Inside a function or where ever you're changing the variable
    11. OptionsMenu.difficulty = // Make your change here
    12. }
    Kind of a brief demonstration. Hope it was helpful :)
     
    Sam_Ooi and Loud_Lasagna like this.
  3. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    ThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyouThankyou
     
    MisterSkitz likes this.
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You are welcome, amigo :)