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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Xare · Jul 25, 2013 at 09:18 AM · javascriptguiprefabarray

Edit Prefab, Instantiate, Access Array in Clone

Hello,

I have a random planet generation system that edits a Sphere called PlanetPrefab with a PlanetProperties script attached to it. The PlanetProperties component of the PlanetPrefab includes an empty Array(javascript array) called resources. The planet generator script, before it instantiates the prefab, edits the resources Array(among many other things) to select three random strings from another Array(another javascript array) called t1resources(inside the planet generator script) and then adds those selected strings to the resources Array inside PlanetProperties component of the PlanetPrefab. However, when I try to access the information stored within the resources Array of every cloned PlanetPrefab, I get an empty Array and there is my problem.

Comment
Add comment · Show 3
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 Xare · Jul 25, 2013 at 09:15 AM 0
Share
 //and here is my code that edits the prefab
 var Planet:GameObject;
 var t1resources=new Array("Lithium","Helium","Bor","Flor","Alu$$anonymous$$um","Steel","Silicium","Carbon","$$anonymous$$","$$anonymous$$ethane");
 function Start () {
 for(var m:int=0;m<3;m++)
                 {
                     Planet.GetComponent(PlanetProperties).resources[m]=t1resources[Random.Range(0,t1resources.length-1)];
                     
                 }
 Instantiate(Planet,Vector3($$anonymous$$athf.Cos(angle)*radius+0,0,$$anonymous$$athf.Sin(angle)*radius+0),Quaternion.identity);
 }
avatar image Xare · Jul 25, 2013 at 09:16 AM 0
Share
 //here is the code inside the PlanetProperties component
 
 var resources=new Array();
avatar image Xare · Jul 25, 2013 at 09:17 AM 0
Share
 //and here is the code that tries to access the array inside the planetproperties component of a cloned object
 var thingy=GameObject.Find("Selection Handler").GetComponent(ObjectSelected).selectedObject;
 GUI.Label(Rect(Screen.width/2+30,Screen.height-90,40,20),thingy.GetComponent(PlanetProperties).resources.ToString());

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Jul 25, 2013 at 09:31 AM

you may need to set the length of the array first. adding this line may do it:

  var resources=new Array();
  resources.length = 3;


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 Xare · Jul 25, 2013 at 11:10 AM 0
Share

Ok here's what I did:

 //changed the resources Array in PlanetProperties
 var resources:String[];

And manually changed the size of the Array attached to the Prefab to 3 in Unity. Now, when I run the game, it changes the elements of the Array in the Prefab but when I try to access the arrays in the clones I get System.String[]

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

16 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 avatar image

Related Questions

Load Prefabs in Array with Javascript 1 Answer

drawing GUI or Instantiate an object to highlight multiple object 2 Answers

Assign "Colour" to Prefab -1 Answers

Resizeable gameObject array. 1 Answer

What type of Array should I use? 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