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. Dismiss Notice

Variable in GetChild

Discussion in 'Scripting' started by livesi5e, Jan 3, 2021.

  1. livesi5e

    livesi5e

    Joined:
    Jan 3, 2021
    Posts:
    2
    Hey, I want to use a variable in the brackets of the geChild command, but I get the error: "Cannot implicitly convert type 'float' to 'int'. An explicit conversion exist (are you missing a cast?)" Here is the Code
    Code (CSharp):
    1. public int Draw = 0f;
    2. public Transform Deck;
    3. public Transform Drawing;
    4.  
    5.     public void Draw1()
    6.     {
    7.         Draw = Random.Range(0, DeckSize);
    8.         Drawing = Deck.gameObject.transform.GetChild(Draw);
    9.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Read the error. You need to remove the f. You are trying to put a float in to an int... which is exactly what that error is telling you.
     
    livesi5e likes this.
  3. livesi5e

    livesi5e

    Joined:
    Jan 3, 2021
    Posts:
    2
    Ah thanks, now it works!