Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

{Solved} How to return int values to a different script?

Discussion in 'Scripting' started by Whyamiworking, Dec 29, 2015.

  1. Whyamiworking

    Whyamiworking

    Joined:
    Apr 2, 2014
    Posts:
    2
    Hi,
    So, I am making a random unique number generator and have one script with this in it :

    Code (CSharp):
    1. public int setInts() {
    2.  
    3.         int firstRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    4.         int secondRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    5.         int thirdRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    6.         int fourthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    7.         int fifthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    8.         int sixthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    9.         int seventhRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    10.         int eighthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    11.         int ninthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    12.         int tenthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    13.         int eleventhRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    14.         int twelfthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    15.         int thirteenthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    16.         int fourteenthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    17.         int fifteenthRand = selfObject.GetComponent<RandomDeck>().DrawRandom();
    18.     //and so on, up to 25
    19.  
    20.     }
    This calls another script for each variable that contains this :

    Code (CSharp):
    1. public int DrawRandom() {
    2.         int index = Random.Range(0, currentOptions.Count);
    3.         int selectedOption = currentOptions[index];
    4.         currentOptions.RemoveAt(index);
    5.         Debug.Log("DrawRandom");
    6.         return selectedOption;
    7.     }
    However, I cannot seem to return the int 'selectedOption' to the random number variables in the other script. I have tried to change the type of the function around, but cannot for the life of me figure out why this is not working.
    The Unity Console error says 'not all code paths return a value'.

    Thanks in advance.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Your setInts method has a return type of int, I assume you will probably want to change that to a void return type.
     
  3. Whyamiworking

    Whyamiworking

    Joined:
    Apr 2, 2014
    Posts:
    2
    But then I cannot get a return from the other script, can I?
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Yes you can, you only need to set a return type on the function that is providing a return value, in this instance your DrawRandom function.

    SetInts doesn't need to return a value, so set it's return type to void.