Question by
Skippy1300 · Mar 09, 2016 at 10:53 PM ·
nullreferenceexceptioncomponentobject reference
Object reference not set to an instance of an object
Ok, so I've tried every way I possibly know of to fix this issue but I've gotten nowhere. I thought GetComponent would fix this but it's not. The error is caused by the debug log message.
using UnityEngine;
using System.Collections;
public class Block : MonoBehaviour {
public Player player;
public int hp;
public int id;
public int moneyGet;
// Use this for initialization
void awake () {
player = (Player)GameObject.FindGameObjectWithTag ("Player").GetComponent (typeof(Player));
}
// Update is called once per frame
void Update () {
}
public void DamageBlock (int loss) {
hp -= loss;
if (hp <= 0) {
Debug.Log(player.moneyEarned);
gameObject.SetActive (false);
}
}
}
Comment
Answer by Zoogyburger · Mar 10, 2016 at 12:26 AM
So are you trying to find the player gameobject or the player script? If you want to find the script all you need is
private Player player;
void Start(){
player = FindObjectOfType<Player> ();
}
If you want to find the gameobject, just put:
public Transform Player;
public void Start()
{
Player = GameObject.FindWithTag ("Player").transform;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612071220im_/https://answers.unity.com/themes/thub/images/avi.jpg)