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 /
  • Help Room /
avatar image
0
Question by ShinyTaco · Dec 15, 2015 at 01:05 PM · rigidbodyprefabarrayaddforce

How to AddForce to a Rigidbody from Game Objects Spawned from an Array, One Prefab at a Time?

Hello.

As the question say, I need to add force to a prefabs that are spawned from an array, and they need to be added to the prefab one at a time.

Let's say there's an array of 10 prefabs that are spawned and moving around. The next time the user clicks on the mouse, AddForce should be added to element 0 in the array, then the next time the mouse button is clicked AddForce should be added to element 1 in the array, so on and so forth. This should go all they way through until the end of the array.

What's a good way to go about doing this?

I initially thought that just adding a public bool and flipping that when the mouse button is clicked, but of course this makes it so all elements in the array are affected by the AddForce.

How can I do it so only one prefab is affected on at a time?

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by wibble82 · Dec 15, 2015 at 01:25 PM

Hi

I think you've got your terminology a bit confused. A prefab is a file in unity that acts as a 'template' for an object you can spawn in the world. I think what you're saying is:

  • I have spawned 10 game objects (presumably by instantiating them from prefabs), and stored references to them in an array

  • When the mouse button is first clicked, I want to apply a force to object 0 in the array

  • When it is clicked again, I want to apply a force to object 1

  • etc etc

I suspect what you're after is something along these lines:

 GameObject[] spawned_objects;
 int next_object_idx;
 
 void Start()
 {
     //presumably you spawn objects here using Instantiate and store them in the spawned_objects array?
 
     next_object_idx = 0;
 }
 
 void Update()
 {
     if(Input.GetMouseButtonDown(0))
     {
         //apply a force
         spawned_objects[next_object_idx].rigidBody.AddForce(bla);
 
         //move onto the next object
         next_object_idx++;
 
         //if we've gone past the end of the array, wrap it back around to 0
         if(next_object_idx > spawned_objects.Length)
             next_object_idx = 0;
     }
 }

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 ShinyTaco · Dec 15, 2015 at 01:35 PM 0
Share

Hey, yes, I stand corrected, game objects, not prefabs. Thank you.

I should have clarified, AddForce needs to be done in the FixedUpdate as the spawned game objects need to keep moving over time.

I have a separate function on another script to AddForce that is called when the mouse button is clicked, which works, but again, it needs to be FixedUpdate (of if there is another way to get the same result) so that the movement continues.

Any ideas?

Thanks.

avatar image wibble82 ShinyTaco · Dec 15, 2015 at 01:46 PM 0
Share

Ok I'm with you I think. So you want clicking the mouse to change which object is currently being affected, but you want to apply the force every frame to the object. In that case something along the lines of:

 GameObject[] spawned_objects;
  int next_object_idx;
  
  void Start()
  {
      //presumably you spawn objects here using Instantiate and store them in the spawned_objects array?
  
      next_object_idx = 0;
  }
  
  void Update()
  {
      //in update we just handle detecting a mouse click to change the object we're working with
      if(Input.Get$$anonymous$$ouseButtonDown(0))
      {
          //move onto the next object
          next_object_idx++;
  
          //if we've gone past the end of the array, wrap it back around to 0
          if(next_object_idx > spawned_objects.Length)
              next_object_idx = 0;
      }
  }
 
  void FixedUpdate()
  {
     //in fixed update we apply an actual force
     spawned_objects[next_object_idx].rigidBody.AddForce(bla);
  }

Hope that helps :)

p.s. if an answer is correct - mark it as correct - it means other people with similar questions find it more easily! Obviously if it's not correct, don't :)

avatar image ShinyTaco · Dec 16, 2015 at 10:03 AM 0
Share

Hey,

So the problem I have now is with this line of here:

 spawned_objects[next_object_idx].rigidBody.AddForce(bla);

This script is placed on a control manager and I need to access the rigidbody on the spawned gameobjects.

I'm not really sure how to do this. How do you go about doing that?

Thanks!

avatar image
0

Answer by keraj37 · Dec 15, 2015 at 02:46 PM

Very simply:

 int index = 0;
     GameObject[] objects = new GameObject[10] {ob1, ob2....obj10};
     public void MouseClicked()
     {
         index++;
 
         if (index >= 10)
             index = 0;
 
         objects [index].GetComponent<Rigidbody> ().AddForce (Vector3.up);
     }

So use indexer and increment it when mouse is cliked.

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 ShinyTaco · Dec 16, 2015 at 03:15 AM 0
Share

I'm getting the following error:

error CS0021: Cannot apply indexing with [] to an expression of type `UnityEngine.GameObject'

Any ideas?

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

35 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

Related Questions

Rigidbody AddForce() stops working after walking away for a while 0 Answers

Script makes plane point in general direction but doesn't point fully... read description please! 1 Answer

GetAxis being missed in FixedUpdate work around? 1 Answer

AddForce in direction of different object? 1 Answer

Using OnTrigger to make two trigger objects open a door in unityscript. 0 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