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 gamerguy · Oct 19, 2013 at 03:52 AM · instantiaterunning

code intended to instantiate 7 PathFloorUnit Prefabs,creates only one clone.

I wrote the following code to instantiate the PathFloorUnit Prefab (meant to form path in my running game) and reference it to the Path array.This code instead only forms one instance.So, why is it not working and how do we correct it?

  using UnityEngine;
     using System.Collections;
     
     public class CreateNew : MonoBehaviour {
        //to get reference from original prefab
        public Object PathFloorUnit;
        //gameObject array created to get reference to instances of instantiated prefab
        GameObject[] Path;
        void Start () {
       //To create instance of PathFloorUnit and assign reference to Path GameObject arrray
            for(int x=0;x<=6;x++){
              Path[x]=Instantiate(PathFloorUnit,newVector3(0,0,10*x),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 Benproductions1 · Oct 19, 2013 at 03:53 AM 0
Share

Please indent your code correctly. It's very hard to read.
Adding some comments would also help :)

1 Reply

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

Answer by AjayKhara · Oct 19, 2013 at 05:18 AM

Try this,

 using UnityEngine;
 using System.Collections;
  
 public class CreateNew : MonoBehaviour
 {
 //to get reference from original prefab
     public Object PathFloorUnit;
 //gameObject array created to get reference to instances of instantiated prefab
     GameObject[] Path = new GameObject[7];
 
     void Start ()
     {
 //To create instance of PathFloorUnit and assign reference to Path GameObject arrray
         for (int x=0; x<=6; x++) {
             Path [x] = Instantiate (PathFloorUnit, new Vector3 (0, 0, 10 * x), Quaternion.identity) as GameObject;
         }
     }
 }


Only problem is that you have not initialized the array. At line no 9. You must initialize your array.

Comment
Add comment · Show 7 · 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 gamerguy · Oct 19, 2013 at 05:26 AM 0
Share

yes it works,thanks yaar.But why doesnt only referencing the array and not creating the gameobjects array actually work(as i did before)?

avatar image Benproductions1 · Oct 19, 2013 at 05:28 AM 0
Share

Because, as any other class, you need to initialize it. The only reason you don't need to initialize something like an int is that it's a structure, passed by value, not by reference :)

avatar image gamerguy · Oct 19, 2013 at 05:39 AM 0
Share

I tried putting the initialization in Start function.It still works,Thanks to your explaination.One more thing, though.I tried initialization without putting an index in GameObject constructor.It didnt work either.So, how do I have control on how many Variable I have in the gameobject array in runtime(you see i want to make a running game and want to target even low end hardware android devices say with less RA$$anonymous$$)

avatar image Benproductions1 · Oct 19, 2013 at 05:42 AM 0
Share

Try looking at different types of collection. What you have there is a static collection or Built-in Array. You can't change it's size. You have to define a size when making a new one as well, or it shouldn't compile.

avatar image AjayKhara · Oct 19, 2013 at 06:05 AM 1
Share

Yes, if you are using a built in array you must define the size of the array while initializing. If you do not want to do that, use a generic collection. It gives you more control.

See this, it will be really helpful. http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

Show more comments

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

15 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

Related Questions

I wrote this code to create 7 prefabs and rename the clones thus formed.This Code isnt working as anticipated. 2 Answers

Checking if object intersects? 1 Answer

Instantiated prefab name change 3 Answers

Changing LocalScale in code affecting prefab stored in Assets 1 Answer

Local vs. Global movement 5 Answers


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