Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 SamCoIndustries · Jul 09, 2015 at 04:07 PM · c#gameobjectprefabarrayinspector

Prefab not loading in data from Inspector

Okay. What I'm trying to do is create an array of prefabs, but I'm having trouble properly creating them and placing them in the array. I've tried several things so far, but these two actually compile and don't crash immediately:

 // 1. An array of GameObjects
 public GameObject[] Weapons;
     
 // Setting array size
 Weapons = new GameObject[(int)WeaponBase.WeaponType.NumTypes];
     
 Weapons[0] = GameObject.Find("MachineGun");

 // 2. An array of base-class scripts
 public WeaponBase[] Weapons;
     
 // Setting array size
 Weapons = new WeaponBase[(int)WeaponBase.WeaponType.NumTypes];
     
 Weapons[0] = gameObject.AddComponent<WeaponBase>();

Neither of these options assigns the values from the prefab's Inspector upon creation, but the objects do get created, and are assigned the values within their ctor. I used to be able to Instantiate this prefab before I implemented the array, but since that's not working, nothing else is.

Any help would be appreciated. Thanks!

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
1

Answer by barbe63 · Jul 10, 2015 at 03:36 AM

Your Weapons array is immedialty overriden on the run by this line:

 Weapons = new GameObject[(int)WeaponBase.WeaponType.NumTypes];

Which is creating a new array... What you need to do is:

 public GameObject[] Weapons = new GameObject[(int)WeaponBase.WeaponType.NumTypes]; 
 // in the declaration you can set the size without override what is in inspector

As I'm not sure it would compile because of the (int)WeaponBase.WeaponType.NumTypes not being accesible in the declaration (I honestly don't know) the 2 alternatives you have are:

  • Set the size in script like this: public GameObject[] Weapons = new GameObject[12];

  • Set the size manually in inspector

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 SamCoIndustries · Aug 04, 2015 at 04:51 AM 0
Share

What I ended up doing was your first bullet, the public GameObject[] Weapons = new GameObject[num];

Then I created factory methods that instantiated new weapons and placed them in their respective array slots.

What I think the problem was (sorry it took me so long to reply) was that I had ctors in the derived class attached to the prefabs, and they were being called, thus overriding what was in the inspector, even though they were empty. I took those out and just made an Init() that was called after creation. Everything works now that there's a factory. Thanks for your quick input!

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

Staged Prefab can't "Apply" after adding using Gameobject as a variable 1 Answer

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

Get Script from gameobject knowing only parent script 1 Answer

Can a GameObject inside of an array be made "accessible" from the editor? 2 Answers

Creating an array of prefabs? 4 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