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
1
Question by StianH · Jan 31, 2013 at 10:58 PM · arrayarrays

How do I use array's to spawn gameobjects(C#)?

I am wondering how I am can spawn my prefabs, 3 different squares, to make up a chessboard. I have been looking at some tutorials, but I can't find anything that particularly have helped me to do this, any tips or useful turorials into understanding arrays is helpful.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jan 31, 2013 at 11:51 PM

Not sure if I correctly understood your problem, but suppose that you could declare a public array and use its elements, like this:

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
 
     public GameObject prefabs[]; // populate the array in the Inspector
 
 
         // set the index to select some specific prefab:
         ...
         Vector3 pos = ... // calculate position
         Quaternion rot = ... // define rotation
         int index = ... // select prefab index
         Instantiate(prefabs[index], pos, rot);
         ...
     }
 }

The array prefabs appears as Prefabs in the Inspector: you must set its length and drag the prefabs to the slots.

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 StianH · Feb 01, 2013 at 04:19 AM 0
Share

I've looked at what you showed me here, but I encounter an error on this line: "public GameObject prefabs[];" It won't work for me. I don't have much knowledge when it comes to scripting at all, but I tried writing it like this which didn't give me an error but I don't think it works like you meant it to.

 public GameObject[] prefabs = new GameObject[1];

that gives me 1 item, I just have to declare which item in the script if I think correctly, something like:

 prefabs[0] = "brett";

That's probably wrong, not sure.

To the pos, rot and that. The pos = new Vector3 (f,f,f)? and rot = Quaternion (f,f,f)? About the index, how do I select that and what should I put in after int index = ?

I don't know much about scripting and the tutorials I've found so far on array's are only about listing names not objects and placing, if you know of any tutorial that touches these subjects please let me know.

avatar image
0

Answer by StianH · Feb 01, 2013 at 12:21 AM

Thanks for the answer, to take it abit further. Is there a way to instantiate a chessboard with a different prefab in the center, and edges of the board? say something like this:

  • 2,1,1,1,2

  • 1,1,3,1,1

  • 2,1,1,1,2

the numbers represent a different color and potential property. 1 being standard, 2 and 3 having some different properties/colors.

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 gfvfubb · Feb 01, 2013 at 12:27 AM 0
Share

So what he showed you is a manually set array where you drop things in with the inspector. You can alternatively populate the array by using .push(gameobject).

So either create your attributes in advance and populate them in the inspector, or use a script to create new prefabs with the properties and then push them to the array.

avatar image
0

Answer by Jonde · Jan 28, 2014 at 04:39 PM

Create the array like you wrote there {2,1,1,1,2,1,1,3,1,1,2,1,1,1,2}. Then go it through with for (int i = 0; i < array.Lenght; i++){ instantiate things here }.

Then you just instantiate the game objects you want at each spot. Everytime you instantiate an object, you add 1f (or whatever the width of the object is) unit more to the next objects position.x. Position.Y get's also added but naturally after you have reached the end of each row.

I hope this helps you to figure it out!

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

12 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

Related Questions

adding classes to arrays 2 Answers

Array of variable-length arrays (JS) 1 Answer

Making objects follow each other in a line, and objects ignoring minimum follow distance. 2 Answers

How to merge an arrays into arrays inside an array? 3 Answers

Show an image array in a 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