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
1
Question by Billamu · Aug 06, 2010 at 02:12 AM · javascriptarray

Can I get one array to manage both an object and its script simultaneously?

At the moment I'm using two arrays; one for the cubes I'm managing and one for a script inside of them. Is it possible to get this down to one array only?

private var cubes : GameObject[]; private var cubeScripts : boxmover[]; // The script in question

private var i : int; private var j : int; function Awake () { cubes = GameObject.FindGameObjectsWithTag ("cube"); for (i = 0 ; i<cubes.length; i++) { cubeScripts[i] = cubes[i].GetComponent(boxmover); } }

function Update() { for (i = 0 ; i<cubes.length; i++) { cubeScripts[i].DoUpdate(); } }

Comment
Add comment · Show 5
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 qJake · Aug 06, 2010 at 02:16 AM 0
Share

Why are you even bothering to ask this? You can't possibly think that by having one array ins$$anonymous$$d of two, you'll somehow increase the performance of your game drastically...? Even if you had one array ins$$anonymous$$d of two, you would still need to manage the references somehow, and it would take the same amount of memory, but again, all this is so insignificant that it really doesn't matter.

avatar image Billamu · Aug 06, 2010 at 02:33 AM 0
Share

I don't expect a performance increase, but I thought having it in one array would make it easier to manage. If it doesn't make much difference then I'll continue as is. Thank you for your comment.

avatar image cncguy · Aug 06, 2010 at 03:08 AM 0
Share

Spike I think that your comment is overly harsh and the question is not bad at all - upvote.

avatar image Wolfram · Aug 06, 2010 at 12:43 PM 0
Share

@Billamu: I assume you are just showing an excerpt of your code? Otherwise, if all you're going to do in Update() is calling a cubeScripts.DoUpdate(), I'd completely get rid of that script, and simply move the content of cubeScripts.DoUpdate() to cubeScripts.Update(), which will be called by Unity anyway every frame for every cubeScript.

avatar image Billamu · Aug 06, 2010 at 11:23 PM 0
Share

@Wolfram: I'm developing for Unity iPhone. The script is organising all things that need to be updated through one Update() routine. The idea came from John Grden's speech at the Unity Developer Conference 09 regarding his development of Star Wars: Trench Run.

2 Replies

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

Answer by Eric5h5 · Aug 06, 2010 at 02:57 AM

I would make the "cubes" array local to the Awake function, and use cubeScripts.length in Update. You only need the cubes array for FindGameObjectsWithTag; no point keeping it around otherwise.

Alternately, if the "boxmover" scripts are only attached to those tagged objects anyway, you can use FindObjectsOfType, which would indeed use just one array.

Comment
Add comment · Show 4 · 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 Billamu · Aug 06, 2010 at 03:19 AM 0
Share

Thanks for that. I hadn't thought of using FindObjectsOfType. Just to be clear; if I was also referencing the gameObject itself I would need the two arrays?

avatar image Eric5h5 · Aug 06, 2010 at 03:28 AM 0
Share

@Billamu, yes you would.

avatar image Wolfram · Aug 06, 2010 at 12:34 PM 0
Share

You can always access the game object to which a script is attached to by using cubeScripts.gameObject - no need to remember them in separate array.

avatar image Billamu · Aug 08, 2010 at 01:41 AM 0
Share

@Wolfram, yes of course! For ages I actually thought the GameObject was the parent of the script, but the script is a Class, meaning the Class acts like a parent to the GameObject. One array pointing to the scripts would be enough. Thank you!

avatar image
1
Best Answer

Answer by cncguy · Aug 06, 2010 at 03:06 AM

Class CubeWrapper { var cubeGO : GameObject; var script : CubeScript; function CubeWrapper(in : GameObject) { cubeGO = in; script = in.GetComponent(CubeScript); }

}

create an array of those and access the public variables.

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 Billamu · Aug 06, 2010 at 03:24 AM 0
Share

I think this is what I was looking for... I'm still getting my head around Unity and haven't ventured into using classes. This example is easy to understand. Thank you!

avatar image cncguy · Aug 06, 2010 at 03:44 AM 0
Share

I use this method all the time. It is particularly useful if there are a number of components or scripts you want to access on each gameObject. just add whatever public variables you want and initialize them in the constructor. Saves having lots of arrays of references.

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

Access a String array in one script from another script 0 Answers

Problem with moving objects in array? 2 Answers

Array problem -3 Answers

How to store aspects (position, name, tag) of a game object in an array to be used for reinstantiation? 1 Answer

Increasing And Decreasing An Array Through A Variable 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