• 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 itvarth · Dec 27, 2019 at 06:14 PM · rigidbodyforce

1 script manipulate only 1 object

I have two balls and when i grab one and i try to throw the other ball that is in the floor its also throw i dont know why :c

 public class AddRBForce : MonoBehaviour
 {
     PickableObject PickableScript;
     public float throwForce = 600;
     Rigidbody rg;
 
     void Start()
     {
         rg = this.GetComponent<Rigidbody>();
         PickableScript = FindObjectOfType<PickableObject>();
     }
 
     void LateUpdate()
     {
         if (PickableScript.shootObject && PickableScript.hasPlayer)
         {
             rg.AddForce(Camera.main.transform.forward * throwForce);
         }
     }
 }




Comment
Add comment · Show 2
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 lgarczyn · Dec 28, 2019 at 07:54 PM 0
Share

You likely need to use GetComponent ins$$anonymous$$d of FindObjectOfType if the script is on the same object as the rigidbody.

avatar image itvarth lgarczyn · Dec 29, 2019 at 03:09 AM 0
Share

Its not in the same objects :c i have the pickup scripts in the player and the addforce scripts in the balls and when i try to throw 1 ball, all the other ball They are also throwed

1 Reply

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

Answer by LOSTSOUL86 · Dec 29, 2019 at 11:00 AM

Hi This has to be written in a different way. Every object has its own copy of the script... but you need to remember that all scripts work in the same way.

Example: Lets say you have script called "moveBallIfForceApplied" You put in the script parameter lets say "forceApplied" that moves ball if forceApplied == true. You attached this script to all the balls. Now you apply force (anywhere) making forceApplied = true; Then all your balls will move. Cause all copies of "moveBallIfForceApplied" script work in the same way. Moving ball when forceApplied = true.

In your case 2 balls are checking the script PickableScript waiting for (PickableScript.shootObject && PickableScript.hasPlayer) to be true. But if its true for one ball then it is true for other ball as well.

Solution: It needs to be done other way round To the ball attached "addForceScript" script with only "addForce" method- that will shoot the ball. Dont link it with any other script. In your PickableScript - find balls for example with: GameObject.FindGameObjectsWithTag("Ball1") -(just simple example you can do it better) GameObject.FindGameObjectsWithTag("Ball2") And then in PickableScript just do: Ball1.getComponent().addForce(); or Ball2.getComponent().addForce();

In general in your serial gameobjects that you multiply dont check parameters in global scripts in update, but your global scripts should set parameters of the particular serial gameobject. Hope this is clear

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 itvarth · Dec 29, 2019 at 10:25 PM 0
Share

Thanks :D.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

162 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 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

Simple colliding objects are updated a frame late. 0 Answers

Reading force on rigidbody? 1 Answer

Calculating required force for pushing a body to a desired position at once 0 Answers

AddForce not influenced by rotation 1 Answer

Gameobject is climbing out objects with rigidbody 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