- Home /
"ArgumentOutOfRangeException: Argument is out of range."
Hello.
So I have this code in my RTS space game, which spawns moons and gives planets textures depending on their distance from the sun.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlanetType : MonoBehaviour {
public Texture Hot;
public Texture Terra;
public Texture Cold;
public Texture Gas;
private List<GameObject> Moons = new List<GameObject>();
//Moon---
public GameObject Moon;
private float currDist = 6f;
private float minDist = 8f; // Minimum distance from the previous moon
private float maxDist = 10f;
private GameObject myMoon;
//------
private float RandomFloat2;
private float RandomFloat;
private float RandomFloat3;
private float MoonNumber;
private bool gas = false;
void Start () {
//Moon---
MoonNumber = Random.Range(0,3);
RandomFloat3 = PlayerPrefs.GetFloat("RandomFloat",0f);
PlaceMoon();
//-------
RandomFloat = Random.Range(12.5f,21f);
Vector3 LC = new Vector3(RandomFloat, RandomFloat, RandomFloat);
transform.localScale = LC;
if(transform.localScale.x > 20f){
gas = true;
RandomFloat2 = Random.Range(35f,45f);
Vector3 LC2 = new Vector3(RandomFloat2, RandomFloat2, RandomFloat2);
transform.localScale = LC2;
renderer.material.mainTexture = Gas;
}
}
void PlaceMoon(){
for (float i = 0f; i < MoonNumber; i++) {
currDist = currDist + Random.Range(minDist, maxDist);
Vector3 v3 = Vector3.right * currDist;
v3 = Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up) * v3;
v3 += transform.position;
myMoon = (GameObject) Instantiate(Moon,new Vector3(v3.x, 0 ,v3.z), Quaternion.Euler(0,0,0));
Moons.Add (myMoon);
}
}
void Update () {
//Moon---
if(Moons[0] != null){
Moons[0].transform.RotateAround(transform.position, Vector3.up, Random.Range(11f,15f) * Time.deltaTime);
Moons[0].transform.RotateAround(Vector3.zero, Vector3.up, RandomFloat3 * Time.deltaTime);
}
if(Moons[1] != null){
Moons[1].transform.RotateAround(transform.position, Vector3.up, Random.Range(11f,15f) * Time.deltaTime);
Moons[1].transform.RotateAround(Vector3.zero, Vector3.up, RandomFloat3 * Time.deltaTime);
}
if(Moons[2] != null){
Moons[2].transform.RotateAround(transform.position, Vector3.up, Random.Range(11f,15f) * Time.deltaTime);
Moons[2].transform.RotateAround(Vector3.zero, Vector3.up, RandomFloat3 * Time.deltaTime);
}
//-------
if(Vector3.Distance(new Vector3(0,0,0),transform.position) < 140 & gas == false){
renderer.material.mainTexture = Hot;
}
if(Vector3.Distance(new Vector3(0,0,0),transform.position) >= 140 & gas == false){
renderer.material.mainTexture = Terra;
}
if(Vector3.Distance(new Vector3(0,0,0),transform.position) >= 300 & gas == false){
renderer.material.mainTexture = Cold;
}
}
}
The problem is, that when i have the moon rotation part of the script not commented out, I get the error which you can see in the title, camera glitches out and the planets have no texture, as you can see on the pisture below:
Thanks in advance for the answears.
-Xentarok
P.S - English isn't my native language, so I apologize for any mistakes.
P.S.2- If you have any questions or I didn't esplain something in enough detail, ask me in the comments.
Answer by bubzy · Jul 28, 2013 at 05:27 PM
MoonNumber = Random.Range(0,3);
then you have specified
Moons.Add(myMoon);
then in update() you call
Moons[0]
Moons[1]
Moons[2]
where the Random.Range could end up being '1' you are asking it to access an index that has not been specified
better off with something like
for (int i = 0; i<Moons.Count; i++)
{
if(Moons[i] != null){
Moons[i].transform.RotateAround(transform.position, Vector3.up, Random.Range 11f,15f) * Time.deltaTime);
Moons[i].transform.RotateAround(Vector3.zero, Vector3.up, RandomFloat3 * Time.deltaTime);
}
Damn Ninja xD
Edit : Since we both basically said the same thing at almost the same time, I deleted my post since yours was up first and looks prettier lol