Error CS0120 message

can anyone help me
An object reference is required for the non-static field, method, or property

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


public enum HealType
{
	Direct,
	Explosion
}

public class HealthPickup : MonoBehaviour
{
    PlayerHealth playerHealth;
    public  HealType healType = HealType.Direct;					    // The Heal type - Direct applys Heal directly from the projectile, Explosion lets an instantiated explosion handle Heal
	public float Heal = 100.0f;										// The amount of Heal to be applied (only for Direct Heal type)
        
       
    void Awake()
    {
        playerHealth = GetComponent<PlayerHealth>();
        
    }

    void OnCollisionEnter(Collision col)
	{
		// If the projectile collides with something, call the Hit() function
		Hit(col);
	}
    void Hit(Collision col)
	{

       

        if (PlayerHealth.currentHealth < PlayerHealth.maxHealth)
        {
            // Apply Heal to the hit object if HealType is set to Direct
		    if (healType == HealType.Direct)
		    {
			    col.collider.gameObject.SendMessageUpwards("ChangeHealth", +Heal, SendMessageOptions.DontRequireReceiver);
                Destroy(gameObject);
			    //call the ApplyHeal() function on the enenmy CharacterSetup script
			    if (col.collider.gameObject.layer == LayerMask.NameToLayer("Limb"))
			    {
				    Vector3 directionShot = col.collider.transform.position - transform.position;

				    // Un-comment the following section for Bloody Mess support
				    /*
				    if (col.collider.gameObject.GetComponent<Limb>())
				    {
					GameObject parent = col.collider.gameObject.GetComponent<Limb>().parent;
					CharacterSetup character = parent.GetComponent<CharacterSetup>();
					character.ApplyHeal(Heal, col.collider.gameObject, weaponType, directionShot, Camera.main.transform.position);
				    }
				    */
			    }
		    }
        }

	}
}


FYI line numbers for the error helps :stuck_out_tongue:

but Im guessing

if (PlayerHealth.currentHealth < PlayerHealth.maxHealth)

line 35, thats the class, not your variable playerHealth → devil is in the details.

what should i write

Given your activity since registering six hours ago specifically, encountering errors CS1585, CS0236, CS0176, and CS0120, and the fact that you not only haven’t been able to find the solutions yourself but continue to ask what to write even after the errors have been pointed out, I strongly recommend that you stop what you’re doing and begin a tutorial on basic programming.

This is not an effective way to code. You will never complete anything independently if you cannot fix basic syntax errors. Find a programming tutorial that starts with the absolute fundamentals of coding in C# before attempting more complex projects, such as creating a game in Unity.

1 Like

Um, I already said :stuck_out_tongue:

PlayerHealth is the class name, playerHealth is your variable (you wrote it :P) you need to use that, if you expect values from a class - often seen when you have like GameManager.Instance, you are asking for a static variable, eg a variable across all instances of the class. Now, you may only have 1 player in your game, but its telling you youve referenced the class and yet you have only instance variables so that each player for example can have different health values… which of course is logical, if i get a wound, your health wont go down.

You therefore need to use the instance, which will be hopefully found in your variable

You’re just making typing mistakes. You can fix your own typing mistakes. Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Pay closer attention to whatever tutorial you’re following.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games: