A field initializer cannot reference 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
{
private 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)
public static float currentHealth = playerHealth.currentHealth;
public static float currentArmor = playerHealth.currentArmor;
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);
}
*/
}
}
}
}
}
Lines 20 and 21, you can only assign constant values to fields. You can’t just assign values from other fields.
Just have them be:
public static float currentHealth;
public static float currentArmor:
Then you can assign values to them on Awake. You probably don’t even need those 2 fields in the first place though.
now its says error cs0120
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);
}
*/
}
}
}
}
}
As already posted in your other duplicate thread, you are just making typing mistakes. You will be required to fix ALL of your mistakes. Here is 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.