- Home /
Assigning random numbers to variables in the awake function
using UnityEngine;
using System.Collections;
public class Treestuff : MonoBehaviour {
public float treeenergy;
public float treeleaves;
public float leafsize;
public float treehieght;
void Awake () {
float treeenergy = 15;
float treeleaves = Random.Range(1,100);
float leafsize = Random.Range(1,100);
float treehieght = Random.Range(1,100);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
This is what I have so far and I'm fairly new, so I have no idea what is wrong. All I know is that when the game starts, the variables are not assigned random numbers with a rang of 1-100.
Comment
Best Answer
Answer by Khada · Sep 17, 2012 at 09:05 AM
Replace:
float treeenergy = 15;
float treeleaves = Random.Range(1,100);
float leafsize = Random.Range(1,100);
float treehieght = Random.Range(1,100);
With:
treeenergy = 15;
treeleaves = Random.Range(1,100);
leafsize = Random.Range(1,100);
treehieght = Random.Range(1,100);
The code you have is redefining (remaking) the variables in question. Removing the type syntax (in this case, 'float') will retrieve the already defined variables by those names.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081128im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Edit - Code isolation using random ID numbers C# 1 Answer
Random Numbers and Array Assignments 3 Answers
Loop a Function a Random Number of Times 2 Answers
Randomly generated number 0 Answers
Random.Range(..) not working 1 Answer