Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by jishansiddiquenuran · Aug 01, 2019 at 05:59 PM · instantiatepositionindexoutofrangeexceptionterain

Two Objects Instatiate Same Position??

issue is , i am instantiate a two prefab randomely but both prefab instantiate at the same position??

Code:

 public class ObjectSpawner : MonoBehaviour
 {
     public Terrain terrain;
     public int numberOfObjects;         //15
     private int currentObjects;
     public GameObject[] objectToPlace; // GameObject to place //store two prefab cube and sphere
     private int terrainWidth; // 500
     private int terrainLength; // 500
     private int terrainPosX; 
     private int terrainPosZ; 
 
     void Start()
     {
         
         terrainWidth = (int)terrain.terrainData.size.x;
         Debug.Log("terrainwidth" + terrainWidth);               //500
      
         terrainLength = (int)terrain.terrainData.size.z;
         Debug.Log("terrainlength" + terrainLength);            //500
    
         terrainPosX = (int)terrain.transform.position.x;
         Debug.Log("terrainposx" + terrainPosX);                //0
        
         terrainPosZ = (int)terrain.transform.position.z;      
         Debug.Log("terrainposz" + terrainPosZ);                //0
     }
     // Update is called once per frame
 
     void Update()
     {
         // generate objects
         if (currentObjects <= numberOfObjects)
         {
              // generate random x position
             int posx = Random.Range(terrainPosX, terrainPosX + terrainWidth);
          
             // generate random z position
             int posz = Random.Range(terrainPosZ, terrainPosZ + terrainLength);
           
              // get the terrain height at the random position
             float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));         
             
             for(int i=0;i<Random.Range(posx,posy);i++)   //problem is both(two) object instantiate at a same position
             {
               
                 GameObject newObject = Instantiate(objectToPlace[i], new Vector3(posx, posy, posz), Quaternion.identity);
                 currentObjects += 1;
             
               
             }
         }
     }
 }


what do i wrong and it is given an error array index out of range??

Referance:

Cube position:

cube position

Sphere Position:

alt text

Error:

https://imgur.com/a/jxxShJQ

sphere.png (104.2 kB)
cube-position.png (72.4 kB)
Comment
Add comment
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

1 Reply

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

Answer by Vega4Life · Aug 01, 2019 at 06:09 PM

Probably need it to be more like this:


 void Update()
     {
         // generate objects
         if (currentObjects <= numberOfObjects)
         {
             for (int i = 0; i < objectToPlace.Length; i++)   //problem is both(two) object instantiate at a same position
             {
                 // generate random x position
                 int posx = Random.Range(terrainPosX, terrainPosX + terrainWidth);
 
                 // generate random z position
                 int posz = Random.Range(terrainPosZ, terrainPosZ + terrainLength);
 
                 // get the terrain height at the random position
                 float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
 
                 GameObject newObject = Instantiate(objectToPlace[i], new Vector3(posx, posy, posz), Quaternion.identity);
                 currentObjects += 1;
             }
         }
     }
Comment
Add comment · Show 1 · 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 jishansiddiquenuran · Aug 01, 2019 at 06:20 PM 0
Share

@Vega4Life can u give referance for this ,i want player should be able to feel the low gravity?? how can i do this??

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

152 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated object coordinates from code aren't the same as the coordinates from Inspector?,Inspector Coordinates not the same as GameObject.Find Coordinates? 1 Answer

FPS instantiate objects 1 Answer

Why are the children of the prefab I am instantiating getting the same exact transforms of its parent? 0 Answers

Instantiate a GameObject at the position of one of its child objects 1 Answer

Unity 2D - MoveTowards position not updating, not recgonizing new position 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