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
0
Question by pickledzebra · Jan 19, 2011 at 02:51 PM · gameobjectgetcomponentpropertiesaddcomponentarraylist

Adding Arbitrary Properties to GameObjects

Let's say we are creating an array of GameObjects as we read a text file like so:

masterScript.js

import System; import System.IO; import System.Text.RegularExpressions; var textAsset : TextAsset;

var things : ArrayList; var thingsPrefab : GameObject;

function Awake() {

 var things = new ArrayList();

 if (textAsset == null) return;
 reader = new StringReader(textAsset.text);
 line = reader.ReadLine();
 while (line != null)
 {
     var newThing = Instantiate(thingPrefab,Vector3.zero,Quaternion.identity);
     things.Add(newThing.gameObject); //arraylist

     line = reader.ReadLine();
     // test for reading halt condition
 }

}

Let's say we want to add some arbitrary properties to each of our GameObjects. Let's focus on non-graphical properties to keep things clear. We parse these properties as we read each line.

The way I'm currently doing it, is to add a "blank" script to "store" the properties for each object. So, I attach the blank script and assign the properties to variables.

thingProperties.js

var numBoats : int; var petName : String; var favoriteQuote : String;

function Awake () { //not doing anything yet }

So, inside our reading loop, we end up with this:

while(line != null)
{
   var newThing = Instantiate(thingPrefab,Vector3.zero,Quaternion.identity);
   things.Add(newThing,gameObject);
   newThing.AddComponent("thingProperties");
   newThing.GetComponent("thingProperties").petName = line.Substring(0,10);
   newThing.GetComponent("thingProperties").numBoats = int.Parse(line.Substring(11,2);
   newThing.GetComponent("thingProperties").favoriteQuote = line.Substring(25,80);
}

This is the strained effort of a javascript noob, but it looks wildly inefficient to me. Clearly the thingProperties script could be added to the thingPrefab. But all these "GetComponent"s look like they might be "slow". I'd like to optimize this code.

Any help would be much appreciated.

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
2
Best Answer

Answer by DaveA · Jan 19, 2011 at 03:57 PM

Something like this pseudocode:

var thing : thingProperties; .....

Awake() .... thing = newThing.GetComponent("thingProperties"); thing.favoriteQuote = line.Sub.....

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

No one has followed this question yet.

Related Questions

GetComponent vs AddComponent 3 Answers

Monobehaviour shared properties across single gameobject 2 Answers

How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers

AddComponent for RawImage not functioning as expected 0 Answers

How to get a variable value from another script(C#)? 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