• 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 Jerem0597 · Jan 13 at 11:36 PM · optimization

How to assemble multiple variables in one variable?

I want to give same function to multiple variables, for example:

 public GameObject a;
 public GameObject b;
 public GameObject c;

 void Update()
 {
     a.transform.position = transform.position + new Vector3(0, 0, 10 * Time.deltaTime);
     b.transform.position = transform.position + new Vector3(0, 0, 10 * Time.deltaTime);
     c.transform.position = transform.position + new Vector3(0, 0, 10 * Time.deltaTime);
 }

Instead of that, how could I combine all variables like:

 public GameObject[] go;

 void Update()
 {
     go.transform.position = transform.position + new Vector3(0, 0, 10 * Time.deltaTime);
 }

If I do like that, the console shows "error CS1061: 'GameObject[]' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)"

I guess that I don't know how to use array.

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

Answer by andycodes · Jan 14 at 12:39 AM

You want to iterate the array of objects to apply it to every object:

 public GameObject[] gameObjects
 
 void Update()
 {
     foreach(GameObject go in gameObjects)
     {
         go.transform.position = transform.position + new Vector3(0, 0, 10 * Time.deltaTime);
     }
 }

However I would suggest avoiding creating a new Vector3 in every update if the vector is going to be composed of static components. Instead you can cache the vector and multiply by Time.deltaTime:

 public GameObject[] gameObjects
 
 private Vector3 moveDelta = new Vector3(0, 0, 10);
 
 void Update()
 {
     foreach(GameObject go in gameObjects)
     {
         go.transform.position = transform.position + (moveDelta * Time.deltaTime);
     }
 }


Also, if you want to practice with Arrays, you can check out the Microsoft docs, and Tutorialspoint also has some good coverage of arrays in C#

Comment
Add comment · Show 3 · 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 Jerem0597 · Jan 14 at 02:11 AM 0
Share

In the console, I got "error CS1656: Cannot assign to 'ps' because it is a 'foreach iteration variable'" when I write:

public ParticleSystem[] particleSystems;

 void Start()
 {
     foreach(ParticleSystem ps in particleSystems)
     {
         ps = GetComponent<ParticleSystem>();
         var main = ps.main;
     }
 }

Do you know why?

Also about avoiding creating a new Vector3 in every update because of static components, what do you mean? I googled about static vs non-static variables. It said that static variables aren't restricted of access from everywhere then these of non-static are.

Do it means that Time.deltaTime is a static component because it's a recognized class from everywhere? If yes, why it's bad to mix it with a new Vector3. Could you explain it please?

Thanks!

avatar image andycodes Jerem0597 · Jan 14 at 02:20 AM 1
Share

ps is a ParticleSystem already, so calling GetComponent<ParticleSystem>() doesn't really do much, but also, ps is considered readonly because it is a "foreach iteration variable". You can read more about that here.

As for the static comment: when I was referring to the vector being static, I meant more so to the value of the vector being unchanging rather than being a static Vector3. The Vector3 itself (0,0,10) in your code example doesn't change, only Time.deltaTime, so it would be relatively inefficient to constantly create a new Vector3 when you could simply reuse one that you've already created.

The reason for preferring to avoid creating a new Vector3 each update is due to memory allocation (and subsequent Garbage Collection) having a reputation for being expensive if performed in excess.

avatar image Jerem0597 andycodes · Jan 14 at 03:12 AM 0
Share

I understand that I made twice time to assign ps as ParticleSystem component, so I deleted GetComponent() and it worked.

And about the reference of link which you pinned in your last reply, I understand that it's readonly from a foreach iteration variable so we can't modify it so we have to find another method to avoid it like what they did.

Thanks for all explanations included static variables with too much data that make performance consuming then if it's useless to add it in update section so it's bad to keep it in.

I can accept your answer.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Check what's subtracting FPS? 1 Answer

Windows Phone performance 0 Answers

So what's Mesh.UploadMeshData do exactly? 3 Answers

When opening unity I get the message File Memory Stream Corrupted 0 Answers

Canvas UI Draw Calls 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges