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 /
avatar image
0
Question by elosbarn · Jun 19, 2014 at 12:16 PM · instantiateloopwhilefordo

Can't instantiate in loop - crashes unity

I'm trying to loop through an array of times, and plot those times by instantiating gameObjects as points in the environment, making the coordinates of the spawn the same as the times. So far I can't even instantiate ten objects in a for loop, let alone reference the points as array indexes. Here is the code that's crashing the Unity Engine:

 using UnityEngine;
 
 using System.Collections;
 
 public class plotGraph : MonoBehaviour {
 
     public GameObject cube;
 
     void Start () {
 
         for(int i=0; i <10; i++){
 
             GameObject newCube = Instantiate(cube, new Vector3(i, i, i), Quaternion.
 identity) as GameObject;
 
         }
     }


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 elosbarn · Jun 19, 2014 at 01:40 PM 0
Share

Here is my working solution

public class plotGraph : $$anonymous$$onoBehaviour { public GameObject point; private int[] times = {0,1,4,4,4,5,6,10,7,8}; private float xLocalScale=0.2f; void Start() { int arrLen = times.Length; for(int i=1; i <arrLen; i++){ GameObject point = GameObject.CreatePrimitive(PrimitiveType.Sphere); point.transform.localScale = new Vector3(0.2f,0.2f,0.2f); point.transform.position = new Vector3(i, times[i], 0); if(times[i-1]-times[i] < -2){ point.renderer.material.color = new Color(10,0,0,10); // } } } }

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Americus · Jun 19, 2014 at 12:28 PM

I'd move it to update or an inum, first of all. But I've had the same problem with for statements, to be honest. I'm going to keep watching for possible reasons why this never works, but one solution is to simply write out what the for statement is doing manually, for example (pseudo c#):

 private int cap = 10;
 private int i = 0;

 if (i < 10)
 {
  Instantiate...
  i++;
 }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help With "For Loop" Not Working? 2 Answers

Using for as while. 3 Answers

Im trying to make an Timer within a for loop, cant figure it out. 1 Answer

For Loop inside While Loop won't Repeat 0 Answers

For loop with Vector3.distance does the loop like 20 times per loop 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