Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to using C#, and i'm trying to make a basic player controller for a 2d game. Left and right movements work properly with this although there is a problem with the "Flip" function (A function i've created to flip the player sprite on the horizontal axis to face where he is moving) at multiple places.

one one line there was apparently expected to be two semicolons when I can't find a reason for that being, and one one line I am not writing something right and it is giving me an error that "Invalid expression term "="

I will bold the comments in which these errors appear.

C++
using UnityEngine;
using System.Collections;

public class TyroneController : MonoBehaviour {

	public float maxSpeed = 10f;
	bool facingRight = true;
	Rigidbody2D myRigidbody2D;



	void Start (){
		myRigidbody2D = this.GetComponent<Rigidbody2D>();



	}


	void FixedUpdate ()
	{
		float move = Input.GetAxis ("Horizontal"); //expected ;

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

		if (move > 0 &&!facingRight)
			Flip ();
		else if (move < 0 && facingRight)
			Flip ();
	}

	void Flip ()
	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1; //invalid expression term "="
		transform.localScale = theScale;
	}

}


What I have tried:

Reviewing the code, and looking up proper c# syntax.
Posted
Updated 3-Apr-16 6:16am
Comments
Homero Rivera 2-Apr-16 20:20pm    
could it be the space in between Input.GetAxis ...and... ("Horizontal");
??? C# is pretty strict.
Kornfeld Eliyahu Peter 3-Apr-16 7:40am    
???
Kornfeld Eliyahu Peter 3-Apr-16 7:41am    
What happens if you replace this
theScale.x *= -1;
with this
theScale.x = theScale.x * -1;

1 solution

There is nothing wrong with the code are you sure that is the exact code you have?

If this is in monodevelop then I suggest you restart unity because that code is fine.

The only possibility I can think of is your script file isn't called TyroneController.cs . I remember unity is very pedantic about about the classname having to match the filename but I thought it gave a specific error to that effect.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900