Operation "==" Cannot be assign to operands of type "string" and "Float" ???

Hi , I am beginner in C# scripting I Got a Script from my friend and he say’s that this will help you to control the object motion in easy way. When I apply this script to a player object the script give a error saying that
"Operation “==” Cannot be assign to operands of type “string” and “Float” ???"
My player Has a rigidbody attacked and I don’t know what is the problem. Please help me I am the beginner xD

Here is My Script :-
//----------------------------------------------------------------//
using UnityEngine;
using System.Collections;
public class Easy : MonoBehaviour {
// here public means that we can access or Change the GameObject with our own object on Unity any time
// < GameObject obj > means that we can add any Object to variable because is now a GameObject
public GameObject obj;
// < Rigidbody rb > means that we can add any Rigidbody of any Object to variable because is now a Rigidbody
Rigidbody rb;
// Here We Created the Strings x,y,z,forward
private string x;
private string y;
private string z;
private string forward;
//--------------------------------//
// Use this for initialization
void Start () {

rb = obj.GetComponent ();
// Here we are giving the Values to the Strings that we created in the above
x = “x”;
y = “y”;
z = “z”;
forward = “forward”;
//------------------------------------//
}

// Update is called once per frame
void Update () {
if (Input.GetKey (“up”)) {
// Here we are calling the Go-Function that we created below
// Go is the function and means we are adding <0.5f> to the object z-axis
Go(z,0.5f);
}
if (Input.GetKey (“down”)) {
// Go is the function and means we are adding < -0.5f > to the object z-axis
Go(z,-0.5f);
}
if (Input.GetKey (“left”)) {
// Go is the function and means we are adding < -0.5f > to the object x-axis
Go(x,-0.5f);
}
if (Input.GetKey (“right”)) {
// Go is the function and means we are adding <0.5f> to the object x-axis
Go(x,0.5f);
}
}
// This function will check where to move the object along vectors or by applying Force
public void Go(string Var,float X){
// THis will add force along the player z-axis Facing At the force of X value
if (Var == forward) {
rb.AddForce(transform.forward * X);
} else
// This will move the object along x-axis At the speed of X value
if (Var == X) {
obj.transform.position += new Vector3 (X, 0f, 0f);
} else
// This will move the object along y-axis At the speed of X value
if (Var == y) {
obj.transform.position += new Vector3 (0f, X, 0f);
} else
// This will move the object along z-axis At the speed of X value
if (Var == z) {
obj.transform.position += new Vector3 (0f, 0f, X);
}
}
}
//----------------------------------------------------------------//
Thanks

if (Var == X) {

thats meant to be lowercase x…

try String1.Equals(String2) when comparing strings.

Thanks LeftyRighty
You are right its not Capital X. its lowercase x and worked also

and sorry for tag I didn’t know this

Thanks vikankraft for giving feedback your are also right
I am a lol i didn’t understand this because i am newby.

== for strings are fine. The only difference between “a.Equals(b)” and “a == b” for strings is that .Equals blows up if a is null.

@Idreeskhan ; this is why variable naming is important. In your Go method:

public void Go(string Var,float X)

the “Var” string is “the direction I should go”, and the “X” float is “how long I should go”. So you should name them to show that:

public void Go(string direction, float distance)

That’ll make reading your code a lot easier, both for yourself when you need to change it later, and for other people who would read it.

1 Like

Ok, but msdn recomends you avoid using == operator, but thats just because its very unclear what you are comparing… I guess.

Probably because == is a general comparator. They each have their benefits and drawbacks. Personally, I prefer == because I’m never unclear on what I’m comparing.

You might never be unclear but for someone reading your code it might be :slight_smile:

Fair point. There are some very complicated scripts, where an observer might become confused about what data types are being compared at some point. However, I’m too new to scripting to make such complicated, fandangled contraptions of code, so I have little to worry about, lol

Right you are.