Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Xentarok · Jul 28, 2013 at 04:59 PM · errorspaceplanetsmoon

"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:

alt text

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.

problem.jpg (80.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Jul 28, 2013 at 07:33 PM 0
Share
  • for description of issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);
     }
 
 
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Jul 28, 2013 at 05:28 PM 1
Share

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

avatar image Xentarok · Jul 28, 2013 at 05:36 PM 1
Share

Yes, it worked, thank you very much :D

avatar image bubzy · Jul 28, 2013 at 05:37 PM 1
Share

haha kisses awesome typing fingers

avatar image clunk47 · Jul 28, 2013 at 07:32 PM 0
Share

Good answer, +1.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Script Editor Not Working 1 Answer

Errors while assigning material to other object's by scripting. 1 Answer

Error line 22! Help. i dont know why? 1 Answer

(C#) Problem with animations not playing 1 Answer

Really need help with: Ambiguous reference 'constructor': UnityEngine.GUIContent.constructor(UnityEngine) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges