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 /
This question was closed Sep 22, 2013 at 06:58 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by maRaider · Sep 21, 2013 at 10:32 PM · errorrigidbodytutorial

Errors when following the Rolling Ball script tutorial.

Very new to Unity, but not to making games. I am a Designer/Producer/Artist, not a programmer, though. Sorry if this is a dumb question, but I cannot find the answer when I search, so I am posting.

I am trying to do the Roll-A-Ball tutorial here: http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player. I have followed every step through creation of the script (below) and when he saves his script (about 9:50) he then proudly says that there are no errors in the console. But, I have errors.

I am sure I did something wrong, but cannot for the life of me figure out what. Here's the script:

 using UnityEngine;
 
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         Rigidbody.AddForce(movement);
     }
 }

But I get this error at the end: Assets/Scripts/PlayerController.cs(13,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'

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

  • Sort: 
avatar image
6
Best Answer

Answer by meat5000 · Sep 21, 2013 at 10:34 PM

Try a small r on rigidbody.

Comment
Add comment · Show 4 · 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 maRaider · Sep 21, 2013 at 11:58 PM 0
Share

Wow... I saw the IDE offer R as a preset and took it. Thank you very, very much!

avatar image meat5000 ♦ · Sep 21, 2013 at 11:59 PM 1
Share

You are welcome. Accept the answer if it solved your problem :)

avatar image Gurwinder Singh · Dec 13, 2013 at 05:27 AM 0
Share

what is r

i am similar this problem but i can not understand what u talk

plz help me

avatar image meat5000 ♦ · Dec 13, 2013 at 12:19 PM 1
Share

rigidbody, not Rigidbody.

Use capital R when casting the type, lowercase r when referring to a rigidbody instance.

avatar image
3

Answer by agustina · Feb 27, 2014 at 06:17 AM

I have the same problem as you, then i change Rigidbody into rigidbody, the eror is done, but when i play the scene , the object cant move, how can i fix that?

Comment
Add comment · Show 10 · 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 Noob2Unity · May 01, 2014 at 09:58 PM 0
Share

Here is a copy of a working PlayerController script for that project:

using UnityEngine; using System.Collections;

public class PlayerController : $$anonymous$$onoBehaviour { public float speed; public GUIText countText; public GUIText winText; private int count;

 void Start()
 {
     count = 0;
     SetCountText();
     winText.text = "";
     }

 void FixedUpdate ()
 {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");

     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
                   
     rigidbody.AddForce(movement * speed * Time.deltaTime);
     }
 void OnTriggerEnter (Collider other) 
 {
     if (other.gameObject.tag == "PickUp")
     {
         other.gameObject.SetActive(false);
         count = count + 1;
         SetCountText();
     }
 }
 void SetCountText()
 {
     countText.text = "Count: " + count.ToString();
     if (count >= 12) {
         winText.text = "YOU WON!";
             }
     }

}

Hope it helps

avatar image digital08 · Mar 05, 2015 at 08:31 PM 0
Share

This was my solution.

GetComponent().AddForce (movement);

avatar image steewan96 · Mar 06, 2015 at 11:44 PM 4
Share

Yeah they updated the API without updating the tutorial. If you put the rigidbody.AddForce(movement); AddForce should show up in red. Save your script then switch back to unity and it will tell you that you're using old references and ask you to update. If you do it will switch your code to

 `GetComponent<Rigidbody>().AddForce(movement);`

Hope this helps

avatar image meat5000 ♦ · Mar 06, 2015 at 11:50 PM 0
Share

[UNITY 5] Do not be miscontrued :

The following cache still works as it has always done

 Rigidbody rbody;
 
 // Use this for initialization
 void Start ()
 {
     rbody = GetComponent<Rigidbody>();
 }

Just call your rigidbody something other than 'rigidbody' or you will get an error.

avatar image AleshiaHowell · Mar 13, 2015 at 04:14 AM 3
Share

Hey, Rodney. I'm also new to Unity and figured this out using the documentation.

Before any of the fixedUpdate stuff, you need to initialize the ball -- or Rigidbody -- and give it a name. Then you reiterate that name ins$$anonymous$$d of "rigidbody" before AddForce. Here's my code:

 public class PlayerController : $$anonymous$$onoBehaviour 
 {
     public Rigidbody sam;
     void Start (){
     sam = GetComponent<Rigidbody>();
 }
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         sam.AddForce(movement);
     }
 }

As you can see, I named my ball Sam. Hope this helps!

avatar image Nightwise AleshiaHowell · Nov 10, 2015 at 06:57 PM 0
Share

Aleshia,

I am very new to Unity. After trying about a dozen failed attempts YOURS is the only one that worked on Unity 5 for me. Don't really understand code, but your answer makes sense and worked perfect. Now I have an object named sam.

Thanks so much.

Show more comments

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Error in script 1 Answer

I'm doing the Roll-A-Ball tutorial and I entered the Player Controller script exactly as the tutorial has it, but I put mine in Unitron and errors are occurring that I don't know how to fix. 2 Answers

Error CS1502 Help! 1 Answer

Why is rigidbody.AddForce not working? 3 Answers

Error CS1502 help! 1 Answer


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