Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by scarletshark · Jul 30, 2012 at 04:23 AM · c#gameobjectarrayinstantiation

How do I create / Instatiate an array of GameObjects in C#

I'm trying to create an array of "selectors" which will be used in a 3D menu. However, the following code is not working as I expected:

 public int numSelectors = 5;
 public GameObject[] selectorArr;
 public Transform selector; //selected in the editor
 
 // Use this for initialization
 void Start () {
  selectorArr = new GameObject[numSelectors];
  for (int i = 0; i < numSelectors; i++){
  selectorArr[i] = (GameObject)Instantiate(selector, new Vector3((float)i,1,0), Quaternion.Euler(new Vector3(0,0,0))) as GameObject;
  selectorArr[i].transform.localScale = new Vector3(1,1,1);     //This is where the exception occurs.
  }
 }

When I run this code, I get the following error at the localScale line:

 InvalidCastException: Cannot Cast from source type to destination type.

I can't determine why an array of GameObjects would not be able to be edited as GameObjects. Thoughts?

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

2 Replies

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

Answer by ScroodgeM · Jul 30, 2012 at 06:05 AM

pay more attention to code. exception should be raised one line higher.

Instantiate returns type of object that was given to it input.

You give to this method a Transform and try to cast returned object to GameObject.

so, give to input GameObject too or cast it to Transform on output.

using UnityEngine;
using System.Collections;
public class TestCodeCS : MonoBehaviour
{
    public int numSelectors = 5;
    public GameObject[] selectorArr;
    public GameObject selector; //selected in the editor
    void Start()
    {
        selectorArr = new GameObject[numSelectors];
        for (int i = 0; i < numSelectors; i++)
        {
            GameObject go = Instantiate(selector, new Vector3((float)i, 1, 0), Quaternion.identity) as GameObject;
            go.transform.localScale = Vector3.one;
            selectorArr[i] = go;
        }
    }
}
Comment
Add comment · Show 2 · 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 scarletshark · Jul 30, 2012 at 12:27 PM 0
Share

Thank you for your feedback. The tutorial that I was following used the "Transform" type, and it seemed to work for instantiation. I also find it odd that the Array was allowing me to put non-GameObjects in it when it was initialized as an array of GameObjects. For instance, in my code above, if I comment out the selectorArr[i].Transform... line, it runs properly (which is why I assumed that is where the exception occurred).

avatar image ScroodgeM · Jul 30, 2012 at 12:34 PM 1
Share

this line is not one i needn't 8) this way lets me to call array just once ins$$anonymous$$d of twice 8) little more performance 8)

avatar image
0

Answer by mcroswell · Dec 27, 2012 at 09:21 PM

Your code was fine except for the double-cast. Either use "(GameObject)" or "as GameObject", not both. Let me know. Just browsing here.

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

9 People are following this question.

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

Related Questions

How to put gameObjects to the list? 4 Answers

How to modify specific GameObjects in an array of GameObjects? 1 Answer

Make an array of all visible objects 1 Answer

2D array of GameObjects C# 2 Answers

How do i change a sprite when another gameobject with the same prefab is colliding / is near 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