MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.GameObject.GetComponent[ChopScript] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:38)
PlayerScript.OnTriggerStay (UnityEngine.Collider other) (at Assets/PlayerScript.cs:20)
Thats what it says when i cut down the first tree wondering if there is a way to do it with tags or just another way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour {
public GameObject tree;
public bool axe;
public float moveSpeed;
public Canvas CanvasObject;
void OnTriggerStay(Collider other)
{
if (axe == true)
{
if (Input.GetMouseButtonDown(0))
{
tree.GetComponent().strength = tree.GetComponent().strength - 1;
}
}
}
void Start () {
axe = false;
CanvasObject.enabled = false;
moveSpeed = 1f;
}
void Update () {
if (Input.GetKeyDown("1"))
{
axe = true;
CanvasObject.enabled = true;
}
if (Input.GetKeyDown("2"))
{
axe = false;
CanvasObject.enabled = false;
}
transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime, 0f, moveSpeed*Input.GetAxis("Vertical") * Time.deltaTime);
}
}
Second script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChopScript : MonoBehaviour {
public int strength;
void Start () {
strength = 5;
}
void Update () {
if (strength == -0)
{
Destroy(gameObject);
}
}
}
↧