• 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 /
  • Help Room /
avatar image
0
Question by Tussockythree68 · Apr 09, 2016 at 07:34 PM · c#unity 5scripting problemerrorcs0120

I need help working out a C# script with error CS0120

I have recently created the following script to make my character object move in either direction and also flip his sprite when moving left if facing right and visa versa

 //variables are being set
 public float maxspeed = 10f;
 bool facingright = true;




 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
     float move = Input.GetAxis("horizantal");

     UnityEngine.Rigidbody2D.velocity = new Vector2(move * maxspeed, UnityEngine.Rigidbody2D.velocity.y);

     if (move > 0 && !facingright)
         Flip();
     else if (move < 0 && facingright)
         Flip();
 }
 //flips the player when they're facing left
 void Flip()
 {
     facingright = !facingright;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;

 }

}

However i get error CS0120 and I can't figure out how to fix it.

Comment
Add comment · Show 7
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 Tussockythree68 · Apr 10, 2016 at 03:21 AM 0
Share

@gooopil To clarify the error is on line 17 of the script, the one that starts with UnityEngine.Rigidbody2D.Velocity

avatar image gooopil Tussockythree68 · Apr 10, 2016 at 05:45 PM 0
Share

@Tussockythree68

You are trying to modify a property in the class, not on an instance of the class. You need a reference to the instance of Rigidbody you want to modify.

For instance if the script is attached to the same Gameobject you want to move:

Declare a field in your class:

 private Rigidbody2D myRigidbody2D;

then in the Start, retrieve the reference:

 myRigidbody2D = GetComponent<Rigidbody2D>();

You can then replace your line with

 myRigidbody2D.velocity = new Vector2(move * maxspeed, myRigidbody2D.velocity.y);

avatar image Tussockythree68 gooopil · Apr 10, 2016 at 11:00 PM 0
Share

@gooopil

I'm still getting the same error P.S. I did as you said and here is the new script

 //variables are being set
 public float maxspeed = 10f;
 bool facingright = true;
 private Rigidbody2D myRigidbody2D;



 // Use this for initialization
 void Start () {
     myRigidbody2D = GetComponent<Rigidbody2D>();

 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
     float move = Input.GetAxis("horizantal");

     myRigidbody2D.velocity = new Vector2(move * maxspeed, UnityEngine.Rigidbody2D.velocity.y);

     if (move > 0 && !facingright)
         Flip();
     else if (move < 0 && facingright)
         Flip();
 }
 //flips the player when they're facing left
 void Flip()
 {
     facingright = !facingright;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;

 }

}

Show more comments
avatar image Tussockythree68 · Apr 11, 2016 at 01:20 AM 0
Share

@gooopil please tell me what lines need to be edited and how.

avatar image gooopil Tussockythree68 · Apr 11, 2016 at 01:21 AM 0
Share

myRigidbody2D.velocity = new Vector2(move * maxspeed, myRigidbody2D.velocity.y);

avatar image Tussockythree68 · Apr 11, 2016 at 01:26 AM 0
Share

@gooopil Sorry about the last thing asking for what needed editing because I figured it out

Thank you for you help as I have now got the game to launch without an error message.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gooopil · Apr 09, 2016 at 08:12 PM

It would be better to ask the question pinpointing where the error is thrown, and indicating the complete error message.

At any rate, this is incorrect:

 float move = Input.GetAxis("horizantal");

It should be

 float move = Input.GetAxis("Horizontal");
Comment
Add comment · 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

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

157 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

Related Questions

tengo un problema con este script no puedo hacer que salte el personaje en un 2f,hi i have a problem making my chacter jump with this script 0 Answers

How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers

How to stop movement script on void start and resume after. 0 Answers

MFPS MouseLook Issue, Namespace or Directive not found after Gaia import! 0 Answers

[HELP] Following the 2D character controller tutorial from the live training section (C#) 0 Answers

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