• 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 caleb.q.owens · Aug 01, 2013 at 11:36 PM · c#prefabarrays

Instantiating a prefab and storing it in a multi-dimensional array

I have a script called InitGameBoard. I have a multi-dimensional array of game objects declared at the beginning like this:

 public GameObject[,] floorContents=new GameObject[20,20]; 

I am trying to store an Instantiated prefab in the multi-dimensional array like this:

 Vector3 select2ndPos=new Vector3(dieEight,0,dieSix);
 floorContents[dieEight,dieSix]=Instantiate(fireObject,select2ndPos,transform.rotation)as GameObject;

and then I am trying to destroy it and remove it from the scene like this:

 Destroy(floorContents[x,z]);

But it doesn't seem like it is even saving the object in the array, and it isn't giving me any errors. Any idea what I'm doing wrong?

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 Xtro · Aug 02, 2013 at 12:09 AM

Make sure the type of fireObject is GameObject.

If the type is a script class, then use fireObject.gameObject in Instantiate method.

Btw, I don't prefer to use "as" for casting purposes. Because it doesn't throw an exception when there is a type casting mismatch. It just sets it to null silently.

To be able to catch that casting errors, you should use regular casting like...

floorContents[dieEight,dieSix]=(GameObject)Instantiate(fireObject,select2ndPos,transform.rotation);

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 caleb.q.owens · Aug 02, 2013 at 12:34 AM 0
Share

Okay, I think I'm getting closer, you were right about some type of casting mismatch, the values were staying as null. I have fireObject declared like this:

 public GameObject fireObject;

And then I try to instantiate a prefab like this:

  floorContents[3,3]=(GameObject)Instantiate(fireObject,selectPos,transform.rotation);

I have also tried using fireObject.gameObject when instanciating

I keep getting this error: Cannot cast from source type to destination type.

Thanks for the quick help.

avatar image caleb.q.owens · Aug 02, 2013 at 12:57 AM 1
Share

Never$$anonymous$$d, I figured it out, rookie mistake. I somehow had two initGameBoard script components on the same prefab and one of them wasn't assigned fireObject. I removed that Component and it was perfect. Thanks for the help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Instantiating objects, related to another objects 0 Answers

Calling prefabs by name or tag returns null. 1 Answer

Obtaining the Variable of a Prefab in an array. 1 Answer

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Serializating a base class array to a prefab 2 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