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 SamChietGames · Apr 05, 2014 at 10:34 PM · javascriptarraysrigidbodies

Change rigidbody values on mutliple objects (ARRAY)

Hello! I'm working on a game where I need to change the values of multiple objects with the same tag.

The idea is that the rigidbody only moves while the player does.

I have code that works, but only for one object because, despite scouring the forums and documentation for hours, I don't understand arrays.

Essentially the code:

  1. Finds the character controller

  2. Detects if it's moving

  3. If so, finds the object tagged "rbTime" and lets it move.

  4. Else, finds the object tagged "rbTime" and stops it.

The issue is that I need this to work on ALL objects tagged "rbTime", not just one.

Here's my code - can someone help me make it work with all objects tagged and explain why it works?

 #pragma strict
 
 public var player : GameObject;
 
 
 
 var isStopped : boolean = false;
 
 
 
 
 function Start() {
 
 
     
 }
 
 function Update () {
     
     var controller : CharacterController = player.GetComponent(CharacterController);
     if(controller.velocity.magnitude < 1) {
     
     physStop();
     
         
     } else {
     
     physStart();
     
     
     }
 
 }
 
 function physStop() {
 
     
     var b : GameObject = GameObject.FindGameObjectWithTag ("rbTime");
 
     var rb = b.GetComponent(Rigidbody);
     
     if (isStopped == false) {
     
     
     rb.velocity = Vector3.zero;
     rb.useGravity = false;
     rb.isKinematic = true;
     
     isStopped = true;
     
     }
     
 
 }
 
 function physStart() {
 
     var b : GameObject = GameObject.FindGameObjectWithTag ("rbTime");
 
     var rb = b.GetComponent(Rigidbody);
 
     if (isStopped == true) {
     rb.isKinematic = false;
     rb.useGravity = true;
     
     isStopped = false;
     }
     
 
 }


Thanks!

-Sam

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 SamChietGames · Apr 06, 2014 at 10:03 PM 0
Share

Should I comment out the code for you guys?

1 Reply

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

Answer by koray1396 · Apr 06, 2014 at 10:31 PM

  • Use FindGameObjectsWithTag instead of "Object"

  • put them in a list or array

  • apply the method for each item in the array using a loop

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 SamChietGames · Apr 07, 2014 at 12:24 AM 0
Share

Hello!

Thank you for the reply. I do know that I need to use an array, but using a loop is new to me. Can you please show how I would use a loop to apply the method for all objects? If possible, modifying the original script?

avatar image koray1396 · Apr 07, 2014 at 04:21 PM 0
Share

ok, you can use lists. add the following line on top of your script, in order to be able to use lists.

 import System.Collections.Generic;

then define a variable for this list above start();

 var rbTimeObjects : List.<GameObject>;

You would want to store all rbTime objects once on Start, it would be waste of resources if you search for the same all the time. Therefore inside function Start();

 rbTimeObjects =  = new List.<GameObject>(FindGameObjectsWithTag("rbTime"));

However, if these objects will not stay the same, you can check them on every update, putting it right under update.

Now objects with rbTime tag are stored in rbTimeObjects, you can do whatever you need with them.

 foreach(var b: GameObject in rbTimeObjects){
     var rb = b.GetComponent(Rigidbody);
     if (isStopped == false) {
         rb.velocity = Vector3.zero;
         rb.useGravity = false;
         rb.is$$anonymous$$inematic = true;
         isStopped = true;
     }
 }

I'm not a javascript user, and i can not test this at the moment, but this is the general idea. I believe below also would be useful for you.

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

https://unity3d.com/learn/tutorials/modules/beginner/scripting/loops

avatar image SamChietGames · Apr 08, 2014 at 10:53 PM 0
Share

It works great - I had to translate some parts into Javascript, but I get this error:

Cannot convert 'UnityEngine.GameObject[]' to 'System.Collections.Generic.List.'.

When creating the List variable:

 var rbTimeObjects : List.< GameObject > = new List.< GameObject >();

(I assume the same error could be produced in C#) Aside from that, the code is error-free!

Thanks for your help so far!

-Sam

avatar image koray1396 · Apr 09, 2014 at 04:22 PM 0
Share

The error is a conflict of array and list, and probably caused by the following line, sorry about that.

you should note that, FindGameObjectsWithTag returns an array, which is different from a list. Lists are easier to handle.

 rbTimeObjects =  = new List.<GameObject>(FindGameObjectsWithTag("rbTime"));

Ins$$anonymous$$d, you can create a foreach loop for the items tagged with "rbTime".

 foreach (var item: GameObject in FindGameObjectsWithTag("rbTime")){
 rbTimeObjects.Add(item);
 }

I hope it works now.

avatar image SamChietGames · Apr 09, 2014 at 10:56 PM 0
Share

In theory, everything should work fine - in the inspector, it shows that both of the objects tagged "rbtime" have been recognized. The thing is, the velocity stop is not going for both of them - only one. The objects are identical - here is my code; maybe you could shed some light on the situation.

 #pragma strict
 
 import System.Collections.Generic;
 
 public var player : GameObject;
 
 //var prevVelocityx : float = 0f;
 //var prevVelocityy : float = 0f;
 //var prevVelocityz : float = 0f;
 
 var isStopped : boolean = false;
 
 var rbTimeObjects : List.< GameObject > = new List.< GameObject >();
 
 function Start() {
     for (var item: GameObject in GameObject.FindGameObjectsWithTag("rbTime")){
     rbTimeObjects.Add(item);
     }
 
 }
 
 function Update () {
     
     var controller : CharacterController = player.GetComponent(CharacterController);
     if(controller.velocity.magnitude < 1) {
     
     physStop();
     
         
     } else {
     
     physStart();
     
     
     }
 
 }
 
 function physStop() {
 
     
     //var b : GameObject = GameObject.FindGameObjectWithTag ("rbTime");
 
     for(var b: GameObject in rbTimeObjects){
     var rb = b.GetComponent(Rigidbody);
     if (isStopped == false) {
     rb.velocity = Vector3.zero;
     rb.useGravity = false;
     rb.is$$anonymous$$inematic = true;
     isStopped = true;
     }
     }
     
 
 }
 
 function physStart() {
 
     //var b : GameObject = GameObject.FindGameObjectWithTag ("rbTime");
 
     for(var b: GameObject in rbTimeObjects){
     var rb = b.GetComponent(Rigidbody);
     if (isStopped == true) {
     rb.is$$anonymous$$inematic = false;
     rb.velocity = Vector3.zero;
     rb.useGravity = true;
     isStopped = false;
     }
     }
     
 
 }
 
Show more comments

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

22 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

Related Questions

Is this considered bad coding 1 Answer

Instantiate multiple objects from an array 1 Answer

Trying to populate an array with a GameObject 1 Answer

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

For values in array 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