Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FritzPeace · Oct 28, 2015 at 12:49 PM · arrayrigidbody2dchildrigidbody.velocitygetcomponentsinchildren

Setting rigidbody.velocity of all child objects to "x".

I got a script where I spawn mountains for a background. These mountains go to the left, and have a velocity of "x".

I use a mountain spawner script. I instantiate the mountains, make them child of the spawner, and give them a certain velocity. Sometimes I will disable (SetActive(false)) the spawner. At this moment everything is okay, however when I re-enable it, all mountains that were previously placed have now a velocity of 0.

Unless there is a way to bypass this behaviour above, I would like to know how I can set the velocity of all mountains previously spawned to "x".

I tried using the following script:

     void OnEnable()
     {
         Rigidbody2D m[] = GetComponentsInChildren<Rigidbody2D>();
         foreach (Rigidbody2D mnt in m)
         {
             m.velocity = new Vector3(-1 * mountainSpeed, 0, 0);
         }
     }

As you may have guessed, it just doesn't work at all. It gives me an error:

 Error    CS0650    Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.    
 

I do not know how I should do this. When I instantiate the mountains, I do not keep track of them (at least I think not"), and just place them as a child of a spawner.

If you know the solution/answer, and it involves some "complex stuff", I will be glad if you explain what you did, so I will not have to ask this again :D

Thanks for the attention!!!

Comment
Add comment · Show 1
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 MelvMay ♦♦ · Oct 28, 2015 at 06:23 PM 0
Share

Note that in 2D you use Vector2 and not Vector3. Unity will implicitly convert to Vector2 for you but there's simply no need to create Vector3 like this.

3 Replies

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

Answer by MelvMay · Oct 28, 2015 at 01:35 PM

The error actually tells you what is wrong. To declare a managed array, the array rank specifier '[]' precedes the variable's identifier 'm' i.e. 'Rigidbody2D[] m =' and not 'Rigidbody2D m[] ='.

Alternately, just use 'var m = '.

https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

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 FritzPeace · Oct 28, 2015 at 09:32 PM 0
Share

Thanks, this worked! The full script I used is here, if anyone needs to use it, or an example. Thanks everyone else though, for the ideas! @$$anonymous$$elv$$anonymous$$ay http://pastebin.com/s80nevVi

avatar image
1

Answer by Statement · Oct 28, 2015 at 01:41 PM

 Rigidbody2D m[] = ...

Should be

 Rigidbody2D[] m = ...

And then your loop looks stange.

 m.velocity = ...

Should be

 mnt.velocity = ...
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 FritzPeace · Oct 28, 2015 at 09:35 PM 0
Share

Thanks! It worked. And true, the loop was wrong :P In case someone needs it, here is the script I used http://pastebin.com/s80nevVi @Statement

avatar image
1

Answer by arcifus · Oct 28, 2015 at 05:49 PM

I would have the children handle their own velocity. That is each mountain can have a Mountain script attached and can refer to the speed setting in its parent.

 // In the mountain script
 Vector3 fixedVelocity = Vector3.zero;
 Rigidbody rigidbody = null;
     
 void Awake() {
    fixedVelocity =  Spawner.fixedVelocity;
    rigidbody = gameObject.GetComponent<Rigidbody>();
 }
     
 void OnEnable() {
    rigidbody.velocity = fixedVelocity;
 }
 
 // In your spawner script
 static Vector3 fixedVelocity;
 const float mountainSpeed = 10f;
 
 void Awake() {
    Spawner.fixedVelocity = Vector3.left * mountainSpeed;
 }




Comment
Add comment · Show 6 · 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 FritzPeace · Oct 28, 2015 at 08:34 PM 0
Share

Isn't it more expensive to do such? I mean you would use a script, etc. @arcifus

avatar image FritzPeace · Oct 28, 2015 at 09:34 PM 0
Share

I was able to do it using my previous idea. http://pastebin.com/s80nevVi Still, do you think the logic I am using in the script above (the one I just pasted) is more efficient than your idea, or not? How much would I win in performance? @arcifus

avatar image arcifus · Oct 29, 2015 at 05:53 AM 0
Share

I've tweaked the script with performance in $$anonymous$$d. Lets see what the changes imply:

  1. The awake function for each mountain will only be called once during its life time

  2. You will call GetComponent only once for each mountain

  3. You will calculate the intended velocity only once in your Spawner script in its Awake

  4. The only thing that will happen for each enable disable cycle is a direct assignment of a precalculated fixed velocity

If you are using the mountains for a scrolling background, make sure you only instantiate the number of mountains that are required to fill the screen view. After that, reuse existing objects by moving them into the starting position again. Instantiating objects is an expensive operation.

I've written these points above to guide you as to how you should think about performance when coding. Also, always run comparison tests of different approaches by generating a lot of load and watching the fps.

Good luck!

avatar image Statement arcifus · Oct 29, 2015 at 01:38 PM 0
Share
 Vector3 fixedVelocity =  Spawner.fixedVelocity;

You are just setting a local variable which will be discarded. Set the field ins$$anonymous$$d.

 fixedVelocity =  Spawner.fixedVelocity;
avatar image FritzPeace · Nov 04, 2015 at 12:24 PM 0
Share

So I should not instantiate the mountain, and should give its velocity inside the mountain script, then use $$anonymous$$athf to make when it walks "x" distance, put it back to its original position?

avatar image arcifus · Nov 04, 2015 at 04:46 PM 0
Share

You're on the right track.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Code will not compare a child gameobjects tag to another 1 Answer

How would I detect my players velocity when it collides with something? 3 Answers

Create array of children in Start function, use it in Update? 1 Answer

how to create rigidbody2d array? 0 Answers

De-Activating object removes rigidbody velocity 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