- Home /
The question is answered, right answer was accepted
Project #00: Roll-a-Ball Tutorial Error
I am working through the tutorial Project #00: Roll-a-Ball in Learn/Tutorial/Projects. In part two Moving the player I get an error at 09:38 of the video. Can anyone tell me what I am doing wrong here?
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);
}
}
and the error message is
Assets/Scripts/PlayerController.cs(13,27): error CS1061: Type UnityEngine.Rigidbody' does not contain a definition for
Addforce' and no extension method Addforce' of type
UnityEngine.Rigidbody' could be found (are you missing a using directive or an assembly reference?)
I am having the same issue, however my script looks correct when comparing line by line to the project: using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour { void Fixedupdate () { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement);
}
}
Any ideas?? I notice some referenced there is no attachment for the rigidbody object. How do you reference a rigidbody object without recreating it?
-1: This isn't a issue with Unity or the tutorial. It's just a basic reading/writing mistake made by the OP. So the question isn't necessarily unwelcome— it just has very low usefulness to anyone other than the OP.
This is not true. If you read my answer, you will see that the OP got confused because of a deprecated property. In his case it looks like he was using $$anonymous$$onoDevelop, and it did not warn him about this. In $$anonymous$$onoDevelop, code completion, simply won't show the property.
$$anonymous$$ore beginners will encounter this same "problem".
Answer by robertbu · Sep 23, 2013 at 06:18 PM
The function is 'AddForce()' with an upper case 'F', not 'Addforce()'. Both C# and Javascript are case sensitive.
Well my mother always said I sucked at typing and that indeed is the case...THAN$$anonymous$$S SO $$anonymous$$UCH...this was my first attempt at learning program$$anonymous$$g in Unity and I had not enough sleep when I attempted it. AddForce it is!
I just noticed another thing and this is after adding a Speed property to the script. I can scroll with my cursor over the Speed text(the arrows going both ways show) but I cannot just go into the text box to change it. This appears also to be the case for the rigidbody section ,sphere collider but not the transform area..those I can hi-lite and I see blue.Other ones I mentioned I can't get that to appear.
$$anonymous$$e doesn't work even though i write "AddForce"........ help
Answer by stalky47 · Mar 06, 2015 at 05:07 PM
this works
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody rb;
void fixedupdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed * Time.deltaTime);
}
}
I see the tutorial is check with 4.0, and I have Unity 5. Is this why my code does not work? It is line by line an exact match with the lesson. I get the same error too. Thanks! There is a space in movement that I cannot prevent from happening and rigidbody always wants to capitalize the "R".
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour
{
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce (movement);
}
}
rigidbody always wants to capitalize the "R".
Thank you for this very important piece of information I have been searching for.
You need to use GetComponent to reference the rigidbody as the aliases/shortcuts are no longer supported in Unity 5.
Thanks $$anonymous$$eat5000!
The tutorial says tested with 4.0. So could I assume that is I were to use version 4.0, then the tutorials would be correct?
As a beginner, it really helps to have accurate tutorials. What do you seasoned devs recommend?
Should I roll back to 4.0, or should I continue to wing it on 5.0?
mameaddicts: The tutorials are fine, there are a few version differences, but they are usually $$anonymous$$or. I would recommend going through all the available tutorials, never$$anonymous$$d which version of Unity they are made with. Game development and program$$anonymous$$g is all about solving problems, you might actually learn more this way, as it forces you to think and find a solution, as opposed to just mechanically doing step by step.
Pay attention to what your IDE is telling you. In this case, Visual Studio told me that this way of doing things were obsolete/deprecated, it even told me how I should do it, and if you are still unsure how, at the very least you know what to search for in the Unity Script Reference.
I don't use $$anonymous$$onoDevelop, it is not as good as Visual Studio, in a number of ways, Visual Studio 2013 Community Edition is free, I recommend that as an IDE for beginners and professionals, also, UnityVS, which is an extension for Visual Studio that helps with debugging.
https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx
Answer by erikbc · Mar 11, 2015 at 07:40 AM
Property 'UnityEngine.Component.rigidbody' is obsolete: The property has been deprecated. Use GetComponent<Rigidbody>() instead.
So, what you do instead of what's in the tutorial is something like this:
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(movement * moveSpeed * Time.deltaTime);
}
}
Answer by Seizure · Sep 23, 2013 at 05:25 PM
The gameobject that this script is attached to does not have a rigidbody, either add one or reference the gameobject that does have the rigidbody.
Answer by TrixieandMindy · Feb 23, 2015 at 04:18 PM
i think you need this part of the script
rigidbody.AddForce(movement speed Time.deltaTime);
Follow this Question
Related Questions
Raycast tutorials? 1 Answer
Where I could find old Unity 3.5 tutorials? 0 Answers
Book/tutorial for experienced programmers 3 Answers
Custom mouse cursor - looking for a really good tutorial. 1 Answer
Free kick unity3d tutorial 1 Answer