Hello,
with the Skript “RayCastDestroyJoint” i want to change the var “treeHealth” from “TreeController” Skript
my error Code:
Assets/_Chopping/Scripts/RayCastDestroyJoint.cs(30,77): error CS0176: Static member `TreeController.treeHealth’ cannot be accessed with an instance reference, qualify it with a type name instead
using UnityEngine;
using System.Collections;
public class RayCastDestroyJoint : MonoBehaviour
{
public float rayLength = 10;
public GameObject tree;
void Start ()
{
TreeController treeController = GetComponent<TreeController> ();
}
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
Ray ray = new Ray ();
RaycastHit hit;
ray.origin = transform.position;
ray.direction = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(ray,out hit, rayLength))
{
tree = (hit.collider.gameObject);
if(hit.collider.gameObject.tag == "Tree")
{
Debug.Log("hit tree with tree tag");
tree.GetComponent<TreeController>().treeHealth -= 1;
}
}
}
}
}
using UnityEngine;
using System.Collections;
public class TreeController : MonoBehaviour
{
static public int treeHealth = 1;
public FixedJoint hinge;
public GameObject tree;
void Start()
{
tree = this.gameObject;
}
void Update()
{
if(treeHealth <= 0)
{
hinge = GetComponent(FixedJoint);
Destroy(hinge);
}
}
}
You have marked treeHealth as static, therefore it does not belong to the tree instance but to the tree class, so all the trees will share the same health.
If you want each tree to have its own health, as that is what sounds reasonable, just remove the modifier static from “static public int treeHealth = 1;”
If you want for all the trees to share the same health (meaning you kill one tree, you kill them all) keep it as static and instead of “tree.GetComponent().treeHealth -= 1;” write “TreeController.treeHealth -= 1;”.
How I would write it: (make sure the tree has treecontroller component, and also and fixedjoint component). Untested, but looks like it should work. I removed the Update() in the treecontroller, because it seems to me that would create unnecessary overhead, running an update cycle for every single tree every single frame, when at any time your only hitting one tree one time every so often. Also a little null checking, just in case you forgot to add the components to the tree. There may be errors, I didn’t test it. Hope this helps.
using UnityEngine;
public class RayCastDestroyJoint : MonoBehaviour
{
public float rayLength = 10;
public int treeDamage = 1;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, rayLength))
{
if(hit.collider.gameObject.tag == "Tree")
{
TreeController tree = hit.collider.gameObject.GetComponent<TreeController>();
if(tree)
tree.Damage(treeDamage);
else
Debug.Log("Tree Does Not Have TreeController Component");
}
}
}
}
}
using UnityEngine;
public class TreeController : MonoBehaviour
{
int treeHealth = 1;
FixedJoint hinge;
void Start()
{
hinge = gameObject.GetComponent<FixedJoint>();
if(!hinge)
Debug.Log("Tree Does Not Have FixedJoint Component");
}
public void Damage(int dmg)
{
treeHealth -= dmg;
if(treeHealth <= 0 && hinge)
Destroy(hinge);
}
}