-2
        using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public Animator Wall;
    public AudioSource hurtSound;
    public Animator animator;
    public int maxHealth = 100;
    int currentHealth;
    public int points = 0;
    public GameObject camera;
    void Start()
    {
        currentHealth = maxHealth;
    }

    
    public void TakeDamage(int damage)
    {
        currentHealth -= damage;
        animator.SetTrigger("hurt");
        hurtSound.PlayOneShot(hurtSound.clip);
        if(currentHealth <= 0)
        {
            Die();
        }
    }

    void Die(){
        Wall.Play("goDown");
        animator.SetBool("isDead", true);
        GetComponent<Collider2D>().enabled = false;
        this.enabled = false;
        points += 1;
        Debug.Log(points);
        
    }

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

public class door : MonoBehaviour
{
    
    void Update(){
        if(Enemy.points == 3){
            print("yeee");
        }
    }

}

i want to refrence the points int to a diferent script but i cant because of this error An object refrence is required for non-static field, method, or property Enemy.points. here is the points int

public int points = 0;

here is where i am tring to refrence it

void Update(){
        if(Enemy.points == 3){
            print("yeee");
        }
    }

but it doesnt work

help

  • To make it easier for people to help you, can you identify which line causes the error? – gunr2171 Jun 25 '21 at 17:46
  • 1
    You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mre]; providing a MRE helps users answer your question and future users relate to your issue. – gunr2171 Jun 25 '21 at 17:46
  • You need to pass somehow a reference to an instance of Enemy object. – SnowGroomer Jun 25 '21 at 18:00
  • 1
    You need to learn [how to debug](https://idownvotedbecau.se/nodebugging/). – Dour High Arch Jun 25 '21 at 19:14

1 Answers1

0

The problem lies in this line of code

 if(Enemy.points == 3){

You attempt to access the points variable directly from a class and not from an object. You need to get a reference of the enemy game object.

If you don't want to assign them at runtime then take a look at the modified door script

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

public class door : MonoBehaviour
{

   public Enemy myEnemy; //assign this in the inspector
    
    void Update(){
        if(myEnemy.points == 3){
            print("yeee");
        }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
JohnP
  • 63
  • 4