So i have a very weird problem happening here. Basically i have a boolean variable that is private (and has always been private so it was never serialized) that i am i trying to set in a different script and the set function gets called every time i want it to be however when i look at the variable in the Update function of my script it never changes.
using UnityEngine;
using System.Collections;
namespace MedievalMayhem.Weapons {
[RequireComponent(typeof(BoxCollider))]
public class MeleeWeapon : Weapon
{
private BoxCollider _hitZone;
private bool _hitZoneOn = true;
public bool HitZoneOn {
get{
Debug.Log ("Getting hitZoneOn " + this._hitZoneOn);
return _hitZoneOn;
}
set{
Debug.Log ("Setting hitZoneOn " + value);
this._hitZoneOn = value;
}
}
protected override void Start() { // edit 2 changed to protected override void
base.Start ();
Debug.Log ("Setting hitzone");
this._hitZone = GetComponent<BoxCollider> ();
this._hitZone.enabled = this._hitZoneOn;
}
void Update() { //edit 2 removed public
Debug.Log ("Hit zone is: " + this._hitZone.enabled);
Debug.Log ("Setting hitzone enabled to " + HitZoneOn);
this._hitZone.enabled = HitZoneOn;
}
void OnTriggerEnter(Collider hit) { //edit 2 removed public
Debug.Log ("HIT " + hit.tag);
}
}
}
The above is the class that is causing the problem. When I run this I will see something like the following in my editor console:
As you can see from the above output the HitZoneOn:set
function is called 597 times just like the rest of the calls but is split between setting the variable to true
and setting it to false
, however, the HitZoneOn:get
function never returns false. Also if i initially set the _hitZoneOn
to false
instead of true
it will always get false
even though it is set to true
at times.
It is probably worth mentioning that I originally tried to use other functions such as:
public void TurnHitZoneOn() {
this._hitZone.enabled = true;
}
public function TurnHitZoneOff() {
this._hitZone.enabled = false;
}
however with the above code the _hitZone
variable is always null outside of the Start
and Update
functions. It is as if I can’t access and modify any variables outside of my Start
and Update
functions. I do this is various other scripts and I have tried to restart unity and change the variable names and no matter what i do i can’t seem to set any of my variables in this script.
Any ideas as to what is going on here would be greatly appreciated!
Thanks
EDIT:
using UnityEngine;
using System.Collections;
using MedievalMayhem.Weapons;
namespace MedievalMayhem.Entities {
public class PlayerFighting {
[SerializeField] private GameObject weapon;
void Start() {
//starting stuff
}
void Update() {
this.weapon.GetComponent<MeleeWeapon>().HitZoneOn = Input.GetButton("Attack");
}
}
}
EDIT 2: Weapon Script
using UnityEngine;
using System.Collections;
namespace MedievalMayhem.Weapons {
public class Weapon : MonoBehaviour {
[SerializeField] private int _damage;
[SerializeField] private GameObject _dropPrefab;
private bool _droppable;
protected virtual void Start() {
if (this._dropPrefab != null) {
this._droppable = true;
} else {
this._droppable = false;
}
}
public bool IsDroppable() {
return this._droppable;
}
public GameObject GetDropPrefab() {
return this._dropPrefab;
}
}
}
EDIT 3
So I have changed my MeleeWeapon script to be the following:
using UnityEngine;
using System.Collections;
namespace MedievalMayhem.Weapons {
[RequireComponent(typeof(BoxCollider))]
public class MeleeWeapon: Weapon
{
public BoxCollider hitZone;
void Awake() {
hitZone = this.GetComponent<BoxCollider> ();
if (hitZone == null) {
Debug.LogError ("NO BOX COLLIDER");
}
}
void OnTriggerEnter(Collider hit) {
Debug.Log ("HIT " + hit.tag);
}
}
}
and I have changed the line to enable the collider in the PlayerFighting script to the following:
//check if the weapons should be enabled or disabeled
bool input = Input.GetButton("Fire1");
this._right_hand_weapon.GetComponent<WeaponMelee>().hitZone.enabled = input;
and I now get the following error whenever I try to access the hitZone variable:
UnassignedReferenceException: The variable hitZone of WeaponMelee has not been assigned.
You probably need to assign the hitZone variable of the WeaponMelee script in the inspector.
MedievalMayhem.Player.PlayerFighting.Update () (at Assets/Scripts/Entities/PlayerFighting.cs:70)
(PlayerFighting.cs:70 refers to the above line)
I don’t under stand what is going on. I never get the error that says ‘NO BOX COLLIDER’ so It shouldn’t be null but for some reason in other scripts it is.
Maybe it is worth mentioning that this object is a runtime instantiated object using the following:
private GameObject AddGear(GameObject prefab, GameObject parent) {
GameObject child = (GameObject)Instantiate (
prefab,
prefab.transform.position,
prefab.transform.rotation
);
child.transform.parent = parent.transform;
child.transform.localPosition = prefab.transform.position;
child.transform.localRotation = prefab.transform.rotation;
child.transform.localScale = prefab.transform.localScale;
return child;
}
//this is called in the PlayerFighting script
this._right_hand_weapon = this.AddGear (pickUp.GetPrefab (), this._right_hand_weapon_hold);
Just because I have no idea what to do or say and I am rambling on the entire code base im using can be found on my github at: GitHub - Tyler-Gauch/MedievalMayhem: Online multiplayer medieval melee combat game in Unity maybe theres something in the scipts that im failing to mention?
Thanks again for all your help