Answer the question
In order to leave comments, you need to log in
Error object reference not set to an instance of an object. Where is wrong?
Here is a map. It has a cube tagged Job and another one tagged Box.
When a player rolls a box tagged cube into a Job tagged cube,
5 should be added to the money.
BUT, the place of this gives an error object reference not set to an instance of an object.
Please find the errors:
script on the job object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Job : MonoBehaviour {
public float addmoney;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider col)
{
if(col.CompareTag("Box"))
{
PlayerMoney pt = col.GetComponent<PlayerMoney>();
addmoney += pt.money;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoney : MonoBehaviour {
public int money;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Answer the question
In order to leave comments, you need to log in
If you throw out all the garbage from your scripts, then the following remains:
using UnityEngine;
public class Job : MonoBehaviour
{
public float addmoney;
private void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Box"))
{
PlayerMoney pt = col.GetComponent<PlayerMoney>();
addmoney += pt.money;
}
}
}
using UnityEngine;
public class PlayerMoney : MonoBehaviour
{
public int money;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question