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

Question Hi! how do i access a randomly generated string in one script from another script?

Discussion in 'Scripting' started by starpoxs, Nov 30, 2022.

  1. starpoxs

    starpoxs

    Joined:
    Nov 30, 2022
    Posts:
    6
    Basically, i've populated a list with strings, and i randomly pick a string from this list.

    i want to pass this randomly chosen string to other scripts, but i don't know how. would appreciate some help/methods
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
    starpoxs likes this.
  3. starpoxs

    starpoxs

    Joined:
    Nov 30, 2022
    Posts:
    6
    i'll give this all a try later; thank you!
     
  4. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    65
    You could store all your "global" variables in a static instance, addressable by all your other scripts etc



    public class GameManager : MonoBehaviour
    {
    public static GameManager Instance;
    void Awake()
    {
    Instance = this;
    }

    // Special ability Chances
    [System.NonSerialized] public int ScoutChance=20; // Chance to un fog 1-3 hexes around party
    [System.NonSerialized] public int SniffChance=10; // Chance per turn to sniff the exit if you have a bisky
    [System.NonSerialized] public int HungerChance=40; // Chance per move of using a hunger point
    [System.NonSerialized] public int ThiefChance=10; // Chance per move of thief acquiring loot
    [System.NonSerialized] public int ShieldCoolDown=10; // Turns to cool down the human shield ability
    [System.NonSerialized] public int ChanceToReroll=20; // Chance to change blank dice face reroll
    [System.NonSerialized] public int ChanceToMakeStar=20; // chance to turn plain dice face into a star face
    }




    To access from another script, you'd do it like this:

          if ((int)Random.Range(1, 100) > GameManager.Instance.SniffChance) { return; }     // did not sniff![/SIZE]


    People will tell you this is bad practice, but just remind them that OOP is bad ->
    [/SIZE]
     
    starpoxs and Kurt-Dekker like this.