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 Unity user · Mar 15, 2011 at 08:37 PM · errorrigidbodyraycastraycasthitbce0020

Simple AddForce Scirpt help

Hi, I'm tyring to build a script, in Javascript, to have the User select an object(With a Rigidbody),andwhen they let go of a button, it will Add force and torque to the objects rigidbody. Here is My Script so far:

var Range : float; var AddedForce : float = 100; var AddedTorque : float = 10; var InputButton : String = "mouse 0";

private var SelectedObject : Rigidbody; function Update () { var Forward = transform.TransformDirection (Vector3.forward);

 if(Input.GetButton(InputButton))
 {
     if(Physics.Raycast(transform.position, Forward, Range))
     {
         SelectedObject = RaycastHit.rigidbody;
     }

     if(Input.GetButtonUp(InputButton))
     {
         SelectedObject.AddForce(Vector3.forward * AddedForce);
         SelectedObject.AddTorque (Vector3.up * AddedTorque);
     }
 }

}

Here is the error i am recieving: Assets/Script.js(20,53): BCE0020: An instance of type 'UnityEngine.RaycastHit' is required to access non static member 'rigidbody'.

I am not very good with raycast so can you please help me, thankyou.

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 Bunny83 · Mar 15, 2011 at 10:18 PM

You have to provide a variable of type RaycastHit. It's a struct and it doesn't have any static members. Do something like that:

var hit : RaycastHit;
if(Physics.Raycast(transform.position, Forward, hit, Range))
{
    SelectedObject = hit.rigidbody;
}
else
{
    SelectedObject = null; // if we aren't hit something reset the selection
}

Take a closer look at Physics.Raycast. There are a lot examples.


edit
Well you should also test if you even have a selected object:

if(SelectedObject != null && Input.GetButtonUp(InputButton))
{
    SelectedObject.AddForce(Vector3.forward * AddedForce);
    SelectedObject.AddTorque (Vector3.up * AddedTorque);
}

When your mouse isn't over an object or the object doesn't have a rigidbody SelectedObject will be initially null. I've also edited my example above and added the else part to reset the selection if you click beside an object or the last one will be selected until you select another one.

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 Unity user · Mar 16, 2011 at 11:28 PM 0
Share

Now it gives me the following error at runtime when i click my mouse:

NullReferenceException UnityEngine.Rigidbody.AddForce (Vector3 force) (at C:/BuildAgent/work/f724c1acfee760b6/Runtime/ExportGenerated/Editor/NewDynamics.cs:491) Push.Update ()

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

No one has followed this question yet.

Related Questions

raycast hit add force problem, help needed please 1 Answer

Rigidbody.position causes shaking 1 Answer

Can't use Rigidbody.AddForce to RaycastHit.point 1 Answer

overloaded Raycast 0 Answers

RaycastHit behind object 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