• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Cancel Input if the direction has a wall in the way 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges