Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

How to access a value / float form another script?

Discussion in 'Scripting' started by hubslash101, Mar 29, 2019.

  1. hubslash101

    hubslash101

    Joined:
    Mar 16, 2019
    Posts:
    26
    I am trying to make a game, and I have a problem. I have 2 scripts with 2 separate randomizers and of course when I try to use something like this:

    Code (CSharp):
    1.         random = UnityEngine.Random.Range(0f, 5f);
    2.         if (random > 0f & random < 1f)
    3.         {
    4.             random = 0f;
    5.             Card1 = 1;
    6.         }
    7.         if (random > 1f & random < 2f)
    8.         {
    9.             random = 0f;
    10.             Card2 = 1;
    11.         }
    12.         if (random > 2f & random < 3f)
    13.         {
    14.             random = 0f;
    15.             Card3 = 1;
    16.         }
    17.         if (random > 3f & random < 4f)
    18.         {
    19.             random = 0f;
    20.             Card4 = 1;
    21.         }
    22.         if (random > 4f & random < 5f)
    23.         {
    24.             random = 0f;
    25.             Card5 = 1;
    26.         }
    In both script, it doesn't come out the same.

    Does anyone know how to copy my value called "random", to another script?
    Thanks!

    Full script 1 (I want the value from the 2nd one to this one):
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MouseDetectObject : MonoBehaviour
    7. {
    8.     public float Card1;
    9.     private float Card2;
    10.     private float Card3;
    11.     private float Card4;
    12.     private float Card5;
    13.     private float Card6;
    14.     private float Card7;
    15.     private float Card8;
    16.     private float Card9;
    17.     private float Card10;
    18.     private float Card11;
    19.     private float Card12;
    20.     private float Card13;
    21.     private float Card14;
    22.     public GameObject card1;
    23.     private GameObject card2;
    24.     private GameObject card3;
    25.     private GameObject card4;
    26.     private GameObject card5;
    27.     Ray ray;
    28.     RaycastHit hit;
    29.     Vector2 mousePosition;
    30.     private IEnumerator wait;
    31.     public Vector2 card1Position;
    32.     public Vector2 card2Position;
    33.     public Vector2 card3Position;
    34.     public Vector2 card4Position;
    35.     public Vector2 card5Position;
    36.     private float cardsActive;
    37.     private float random;
    38.     public bool test = true;
    39.     private bool gameEnd = false;
    40.     private bool AdvandingRound = false;
    41.     private bool waited = false;
    42.     private bool waited2 = false;
    43.     public bool Card1Selected = false;
    44.     private bool Card2Selected = false;
    45.     private bool Card3Selected = false;
    46.     private bool Card4Selected = false;
    47.     private bool Card5Selected = false;
    48.     public bool card1Found = false;
    49.     private bool card2Found = false;
    50.     private bool card3Found = false;
    51.     private bool card4Found = false;
    52.     private bool card5Found = false;
    53.     public bool startFindingCard1 = false;
    54.     private bool startFindingCard2 = false;
    55.     private bool startFindingCard3 = false;
    56.     private bool startFindingCard4 = false;
    57.     private bool startFindingCard5 = false;
    58.     public bool noBoxcollider1 = true;
    59.     private bool noBoxcollider2 = true;
    60.     private bool noBoxcollider3 = true;
    61.     private bool noBoxcollider4 = true;
    62.     private bool noBoxcollider5 = true;
    63.  
    64.     void Start()
    65.     {
    66.      
    67.     }
    68.  
    69.     void Update()
    70.     {
    71.         if (Input.GetKeyDown("space") & AdvandingRound == false)
    72.         {
    73.             AdvanceRound();
    74.         }
    75.  
    76.         if (Card1 == 1 && card1Found == false)
    77.         {
    78.             startFindingCard1 = true;
    79.         }
    80.         else
    81.         {
    82.             startFindingCard1 = false;
    83.         }
    84.         if (Card2 == 1 && card2Found == false)
    85.         {
    86.             startFindingCard2 = true;
    87.         }
    88.         else
    89.         {
    90.             startFindingCard2 = false;
    91.         }
    92.         if (Card3 == 1 && card3Found == false)
    93.         {
    94.             startFindingCard3 = true;
    95.         }
    96.         else
    97.         {
    98.             startFindingCard3 = false;
    99.         }
    100.         if (Card4 == 1 && card4Found == false)
    101.         {
    102.             startFindingCard4 = true;
    103.         }
    104.         else
    105.         {
    106.             startFindingCard4 = false;
    107.         }
    108.         if (Card5 == 1 && card5Found == false)
    109.         {
    110.             startFindingCard5 = true;
    111.         }
    112.         else
    113.         {
    114.             startFindingCard5 = false;
    115.         }
    116.  
    117.         if (card1Found == false && startFindingCard1 == true)
    118.         {
    119.             if (GameObject.Find("Card1O") == true)
    120.             {
    121.                 card1Found = true;
    122.                 card1 = GameObject.Find("Card1O");
    123.                 card1StartPosition();
    124.                 startFindingCard1 = false;
    125.             }
    126.         }
    127.         if (card2Found == false && startFindingCard2 == true)
    128.         {
    129.             if (GameObject.Find("Card2O") == true)
    130.             {
    131.                 card2Found = true;
    132.                 card2 = GameObject.Find("Card2O");
    133.                 card2StartPosition();
    134.                 startFindingCard2 = false;
    135.             }
    136.         }
    137.         if (card3Found == false && startFindingCard3 == true)
    138.         {
    139.             if (GameObject.Find("Card3O") == true)
    140.             {
    141.                 card3Found = true;
    142.                 card3 = GameObject.Find("Card3O");
    143.                 card3StartPosition();
    144.                 startFindingCard3 = false;
    145.             }
    146.         }
    147.         if (card4Found == false && startFindingCard4 == true)
    148.         {
    149.             if (GameObject.Find("Card4O") == true)
    150.             {
    151.                 card4Found = true;
    152.                 card4 = GameObject.Find("Card4O");
    153.                 card4StartPosition();
    154.                 startFindingCard4 = false;
    155.             }
    156.         }
    157.         if (card5Found == false && startFindingCard5 == true)
    158.         {
    159.             if (GameObject.Find("Card5O") == true)
    160.             {
    161.                 card5Found = true;
    162.                 card5 = GameObject.Find("Card5O");
    163.                 card5StartPosition();
    164.                 startFindingCard5 = false;
    165.             }
    166.         }
    167.  
    168.         //Card 1
    169.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    170.         if (Physics.Raycast(ray, out hit) & Input.GetMouseButton(0) && Card2Selected == false & Card3Selected == false && Card4Selected == false && Card5Selected == false)
    171.         {
    172.             if (hit.collider.tag == "Card1")
    173.             {
    174.                 Card1Selected = true;
    175.                 print(hit.collider.name);
    176.                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    177.                 card1.transform.position = mousePosition;
    178.             }
    179.         }
    180.         if (Input.GetMouseButtonUp(0) & Card1Selected == true && test == true)
    181.         {
    182.             card1.transform.position = card1Position;
    183.             Card1Selected = false;
    184.         }
    185.         //Card2
    186.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    187.         if (Physics.Raycast(ray, out hit) & Input.GetMouseButton(0) && Card1Selected == false && Card3Selected == false && Card4Selected == false && Card5Selected == false) //continue
    188.         {
    189.             if (hit.collider.tag == "Card2")
    190.             {
    191.                 Card2Selected = true;
    192.                 print(hit.collider.name);
    193.                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    194.                 card2.transform.position = mousePosition;
    195.             }
    196.         }
    197.         if (Input.GetMouseButtonUp(0) & Card2Selected == true && test == true)
    198.         {
    199.             card2.transform.position = card2Position;
    200.             Card2Selected = false;
    201.         }
    202.         //Card3
    203.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    204.         if (Physics.Raycast(ray, out hit) & Input.GetMouseButton(0) & Card1Selected == false && Card2Selected == false && Card4Selected == false && Card5Selected == false) //continue
    205.         {
    206.             if (hit.collider.tag == "Card3")
    207.             {
    208.                 Card3Selected = true;
    209.                 print(hit.collider.name);
    210.                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    211.                 card3.transform.position = mousePosition;
    212.             }
    213.         }
    214.         if (Input.GetMouseButtonUp(0) & Card3Selected == true && test == true)
    215.         {
    216.             card3.transform.position = card3Position;
    217.             Card3Selected = false;
    218.         }
    219.         //Card4
    220.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    221.         if (Physics.Raycast(ray, out hit) & Input.GetMouseButton(0) & Card1Selected == false && Card2Selected == false && Card3Selected == false && Card5Selected == false) //continue
    222.         {
    223.             if (hit.collider.tag == "Card4")
    224.             {
    225.                 Card4Selected = true;
    226.                 print(hit.collider.name);
    227.                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    228.                 card4.transform.position = mousePosition;
    229.             }
    230.         }
    231.         if (Input.GetMouseButtonUp(0) & Card4Selected == true && test == true)
    232.         {
    233.             card4.transform.position = card4Position;
    234.             Card4Selected = false;
    235.         }
    236.         //Card5
    237.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    238.         if (Physics.Raycast(ray, out hit) & Input.GetMouseButton(0) & Card1Selected == false && Card2Selected == false && Card3Selected == false && Card4Selected == false) //continue
    239.         {
    240.             if (hit.collider.tag == "Card5")
    241.             {
    242.                 Card5Selected = true;
    243.                 print(hit.collider.name);
    244.                 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    245.                 card5.transform.position = mousePosition;
    246.             }
    247.         }
    248.         if (Input.GetMouseButtonUp(0) & Card5Selected == true && test == true)
    249.         {
    250.             card5.transform.position = card5Position;
    251.             Card5Selected = false;
    252.         }
    253.  
    254.         if (gameEnd == true)
    255.         {
    256.             gameEnd = true; //popup gui
    257.         }
    258.  
    259.         if (random >= 0.01f && random <= 1f && waited2 == true)
    260.         {
    261.             random = 0f;
    262.             Card1 = 1;
    263.         }
    264.         else if (random >= 1f && random <= 2f && waited2 == true)
    265.         {
    266.             random = 0f;
    267.             Card2 = 1;
    268.         }
    269.         else if (random >= 2f && random <= 3f && waited2 == true)
    270.         {
    271.             random = 0f;
    272.             Card3 = 1;
    273.         }
    274.         else if (random >= 3f && random <= 4f && waited2 == true)
    275.         {
    276.             random = 0f;
    277.             Card4 = 1;
    278.         }
    279.         else if (random >= 4f && random <= 5f && waited2 == true)
    280.         {
    281.             random = 0f;
    282.             Card5 = 1;
    283.         }
    284.     }
    285.     //Card1
    286.     private void card1StartPosition()
    287.     {
    288.         card1.transform.position = card1Position;
    289.         Card1Selected = false;
    290.         if (noBoxcollider1 == true)
    291.         {
    292.             BoxCollider _bc = (BoxCollider)card1.gameObject.AddComponent(typeof(BoxCollider));
    293.             _bc.center = Vector3.zero;
    294.             noBoxcollider1 = false;
    295.         }
    296.     }
    297.     //Card2
    298.     private void card2StartPosition()
    299.     {
    300.         card2.transform.position = card2Position;
    301.         Card2Selected = false;
    302.         if (noBoxcollider2 == true)
    303.         {
    304.             BoxCollider _bc = (BoxCollider)card2.gameObject.AddComponent(typeof(BoxCollider));
    305.             _bc.center = Vector3.zero;
    306.             noBoxcollider2 = false;
    307.         }
    308.     }
    309.     //Card3
    310.     private void card3StartPosition()
    311.     {
    312.         card3.transform.position = card3Position;
    313.         Card3Selected = false;
    314.         if (noBoxcollider3 == true)
    315.         {
    316.             BoxCollider _bc = (BoxCollider)card3.gameObject.AddComponent(typeof(BoxCollider));
    317.             _bc.center = Vector3.zero;
    318.             noBoxcollider3 = false;
    319.         }
    320.     }
    321.     //Card4
    322.     private void card4StartPosition()
    323.     {
    324.         card4.transform.position = card4Position;
    325.         Card4Selected = false;
    326.         if (noBoxcollider4 == true)
    327.         {
    328.             BoxCollider _bc = (BoxCollider)card4.gameObject.AddComponent(typeof(BoxCollider));
    329.             _bc.center = Vector3.zero;
    330.             noBoxcollider4 = false;
    331.         }
    332.     }
    333.     //Card5
    334.     private void card5StartPosition()
    335.     {
    336.         card5.transform.position = card5Position;
    337.         Card5Selected = false;
    338.         if (noBoxcollider5 == true)
    339.         {
    340.             BoxCollider _bc = (BoxCollider)card5.gameObject.AddComponent(typeof(BoxCollider));
    341.             _bc.center = Vector3.zero;
    342.             noBoxcollider5 = false;
    343.         }
    344.     }
    345.  
    346.     private void AdvanceRound()
    347.     {
    348.         random = UnityEngine.Random.Range(0.01f, 5f);
    349.         wait = WaitTime(1f);
    350.  
    351.         AdvandingRound = true;
    352.         cardsActive = Card1 + Card2 + Card3 + Card3 + Card5; //etc
    353.         if (cardsActive >= 12)
    354.         {
    355.             gameEnd = true;
    356.         }
    357.         else if (cardsActive <= 3)
    358.         {
    359.             gameEnd = true;
    360.         }
    361.         else
    362.         {
    363.             AdvandingRound = false;
    364.         }
    365.  
    366.         //add this into else afther testing
    367.         if (random >= 0.01f && random <= 5f)
    368.         {
    369.             StartCoroutine(wait);
    370.         }
    371.     }
    372.     private IEnumerator WaitTime(float waitTime)
    373.     {
    374.         yield return new WaitForSeconds(waitTime);
    375.         print("Card Selected");
    376.         waited2 = true;
    377.     }
    378.  
    379. }
    380.  
    Full script 2 (This is the script i want the value copied from):
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Cards : MonoBehaviour
    7. {
    8.     //O = object S = Spawned
    9.  
    10.     private bool gameEnd = false;
    11.     private bool AdvandingRound = false;
    12.     private float Card1;
    13.     private float Card2;
    14.     private float Card3;
    15.     private float Card4;
    16.     private float Card5;
    17.     private float Card6;
    18.     private float Card7;
    19.     private float Card8;
    20.     private float Card9;
    21.     private float Card10;
    22.     private float Card11;
    23.     private float Card12;
    24.     private float Card13;
    25.     private float Card14;
    26.     private float lockedCard1 = 1;
    27.     private float lockedCard2 = 1;
    28.     private float lockedCard3 = 1;
    29.     private float cardsActive;
    30.     private float Stability = 43;
    31.     private float Money = 200;
    32.     public GameObject Card1O;
    33.     public GameObject Card2O;
    34.     public GameObject Card3O;
    35.     public GameObject Card4O;
    36.     public GameObject Card5O;
    37.     public float random;
    38.  
    39.     void Start()
    40.     {
    41.      
    42.     }
    43.  
    44.     void Update()
    45.     {
    46.         if (Input.GetKeyDown("space") & AdvandingRound == false)
    47.         {
    48.             AdvanceRound();
    49.         }
    50.         if (Card1 == 1)
    51.         {
    52.             GameObject Card1OS = Instantiate(Card1O);
    53.             Card1OS.transform.position = new Vector2(10.38f, -3.3f);
    54.             Card1OS.tag = ("Card1");
    55.             Card1OS.name = Card1O.name;
    56.             //startFindingCard1 = true (transfer to other script)
    57.             Card1 = 0;
    58.         }
    59.         else
    60.         {
    61.          
    62.         }
    63.         if (Card2 == 1)
    64.         {
    65.             GameObject Card2OS = Instantiate(Card2O);
    66.             Card2OS.transform.position = new Vector2(10.38f, -3.3f);
    67.             Card2OS.tag = ("Card2");
    68.             Card2OS.name = Card2O.name;
    69.             Card2 = 0;
    70.         }
    71.         else
    72.         {
    73.  
    74.         }
    75.         if (Card3 == 1)
    76.         {
    77.             GameObject Card3OS = Instantiate(Card3O);
    78.             Card3OS.transform.position = new Vector2(10.38f, -3.3f);
    79.             Card3OS.tag = ("Card3");
    80.             Card3OS.name = Card3O.name;
    81.             Card3 = 0;
    82.         }
    83.         else
    84.         {
    85.  
    86.         }
    87.         if (Card4 == 1)
    88.         {
    89.             GameObject Card4OS = Instantiate(Card4O);
    90.             Card4OS.transform.position = new Vector2(10.38f, -3.3f);
    91.             Card4OS.tag = ("Card4");
    92.             Card4OS.name = Card4O.name;
    93.             Card4 = 0;
    94.         }
    95.         else
    96.         {
    97.  
    98.         }
    99.         if (Card5 == 1)
    100.         {
    101.             GameObject Card5OS = Instantiate(Card5O);
    102.             Card5OS.transform.position = new Vector2(10.38f, -3.3f);
    103.             Card5OS.tag = ("Card5");
    104.             Card5OS.name = Card5O.name;
    105.             Card5 = 0;
    106.         }
    107.         else
    108.         {
    109.  
    110.         }
    111.         if (gameEnd == true)
    112.         {
    113.             gameEnd = true; //popup gui
    114.         }
    115.     }
    116.  
    117.     void LateUpdate()
    118.     {
    119.  
    120.     }
    121.  
    122.     private void AdvanceRound()
    123.     {
    124.         AdvandingRound = true;
    125.         cardsActive = Card1 + Card2 + Card3 + Card3 + Card5; //etc
    126.         if (cardsActive ==+ 12)
    127.         {
    128.             gameEnd = true;
    129.         }
    130.         else if (cardsActive ==- 3)
    131.         {
    132.             gameEnd = true;
    133.         }
    134.         else
    135.         {
    136.             AdvandingRound = false;
    137.         }
    138.         //add this into else afther testing
    139.         random = UnityEngine.Random.Range(0f, 5f);
    140.         if (random > 0f & random < 1f)
    141.         {
    142.             random = 0f;
    143.             Card1 = 1;
    144.         }
    145.         if (random > 1f & random < 2f)
    146.         {
    147.             random = 0f;
    148.             Card2 = 1;
    149.         }
    150.         if (random > 2f & random < 3f)
    151.         {
    152.             random = 0f;
    153.             Card3 = 1;
    154.         }
    155.         if (random > 3f & random < 4f)
    156.         {
    157.             random = 0f;
    158.             Card4 = 1;
    159.         }
    160.         if (random > 4f & random < 5f)
    161.         {
    162.             random = 0f;
    163.             Card5 = 1;
    164.         }
    165.     }
    166. }
    167.  
    Thanks again!
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    308
    That hurts my eyes to look at. Get some Lists and Arrays in there. As for the value just do FindObjectOfType<MouseDetectObject>().value = random;
     
    hubslash101 likes this.
  3. hubslash101

    hubslash101

    Joined:
    Mar 16, 2019
    Posts:
    26
    I am new to coding, I just started 1-2 weeks ago. Can you please tell me what Lists and Arrays are, and maybe how to use them? The "tutorials" aren't really helping, Thanks!
     
  4. hubslash101

    hubslash101

    Joined:
    Mar 16, 2019
    Posts:
    26
    Ps, just linking a good source I can learn from should also be enough :), Thanks again.
     
  5. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    If you've just started coding for the first time 1 or 2 weeks ago, personally I'd stick to some standard C# tutorials for the time being before diving in with Unity. That way you can learn all the fundamentals of classes, lists, arrays, enums, LinQ, etc, before trying to create Unity scripts.

    (Search on Youtube for "Bob Tabor C#" - there's a brilliant 8hr course that covers a whole bunch of vital fundamentals that are an essential precursor to Unity development)
     
    hubslash101 likes this.
  6. hubslash101

    hubslash101

    Joined:
    Mar 16, 2019
    Posts:
    26
    Thanks!