operator cannot be applied to type from getcomponent

new unity and C# user. i appear to be confuddled into a quandary, im using getcomponent() to call on variables from different scripts and im getting the Operator ‘!=’ cannot be applied to operands of type ‘float’ and ‘levelmanager’ wherever applicable in my script here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class badworks : MonoBehaviour {

    private GameObject theTarget;
    public GameObject badGameObject;
    private levelmanager Levelmanager;

    void Awake(){
        Levelmanager = badGameObject.GetComponent<levelmanager> ();
    }
    void Start () {
        float i = 0;
        while (i != Levelmanager.EnemyPerLevel){ // error: Operator '!=' cannot be applied to operands of type 'float' and 'levelmanager'
            Instantiate(badGameObject, new Vector3(6.2f,0.6f,4.3f),Quaternion.identity);
        }
        theTarget = GameObject.FindGameObjectWithTag ("Player");
        if (theTarget == null){
            Debug.Log("WARNING: The Player tag could not be found");
        }
    }
    void oppCollision (Collision c){
        if (c.gameObject.tag == "Bullet") {
            Levelmanager.BadHealth = Levelmanager.BadHealth - Levelmanager.BulletDamage; //error: Operator '-' cannot be applied to operands of type 'levelmanager' and 'levelmanager'
        }
    }
    void Update () {
        if (Levelmanager.BadHealth == 0) { //error: operator '==' cannot be applied to operands of type 'levelmanager' and 'int'
            Destroy (this.gameObject);
        }
        if (theTarget != null){
            transform.LookAt(theTarget.transform.position);
            transform.position += (transform.forward * Levelmanager.BadMoveRate); //error: operator '*' cannot be applied to operands of type 'UnityEngine.Vector3' and 'levelmanager'
        }
    }
}

seems like an ez fix yet i’m currently not sure what’s stirring my sprinkles

What type is the variable Levelmanager.EnemyPerLevel? Is that a float?

And if you are using floats, you should not compare them for equality: look up “floating point errors.”

Also, your capitalization conventions are a bit backwards: Traditionally classes are capitalized:

public class Foo ...

And a private instance of the class would not be:

Foo foo = ...

in the levelmanager script they’re declared like this:

    public levelmanager EnemyPerLevel;
    public levelmanager BulletDamage;
    public levelmanager PlayMoveSpeed;  
    public levelmanager BadHealth;
    public levelmanager BadMoveRate;

im not sure if I have to have to declare the type, but when I do (int for example) it gives me two errors on the same line, “; expected” and “invalid token ‘;’ in class, struct, or interface member declaration”

I agree with Kurt-Dekker. Your capitilisation makes it tricky to read your code. I recommend you read up on coding standards.

In your level manager script, you have declared all the variables to be of type levelmanager??

I think this is what you want:

public int EnemyPerLevel;
public int BulletDamage;
public float PlayMoveSpeed;
public int BadHealth;
public float BadMoveRate;

I have made some guesses for the type to declare each variable. eg: In your first post, you have EnemyPerLevel as a float. But you can’t have 0.5 enemys. So make this an int.

1 Like