NullReferenceException and he referenced script on this Behaviour
Hello i am new to unity and c#. Not be able to resolve my error. i want to count pumpkin but not able to count. My count value only changes from 0 to 1 only. @kush. Waiting for the response. Thanks
Below is my code
GolemController
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GolemController : MonoBehaviour {
private Rigidbody myBody;
private float moveForce = 10f;
void Awake () {
Screen.orientation = ScreenOrientation.LandscapeLeft;
myBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
// for compuetr or desktop
float h = Input.GetAxis ("Horizontal");
myBody.velocity = new Vector3 (-h * moveForce, 0f, 0f);
// for mobile device
//myBody.transform.Translate(Input.acceleration.x, 0 ,0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GolemController : MonoBehaviour {
private Rigidbody myBody;
private float moveForce = 10f;
void Awake () {
Screen.orientation = ScreenOrientation.LandscapeLeft;
myBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
// for compuetr or desktop
float h = Input.GetAxis ("Horizontal");
myBody.velocity = new Vector3 (-h * moveForce, 0f, 0f);
// for mobile device
//myBody.transform.Translate(Input.acceleration.x, 0 ,0);
}
}
Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public Transform[] spawnPoints;
public GameObject pumpkin;
void Start () {
StartCoroutine (StartSpawning ());
}
IEnumerator StartSpawning()
{
yield return new WaitForSeconds (Random.Range (1f, 3.5f));
Instantiate (pumpkin, spawnPoints [Random.Range (0, spawnPoints.Length)].position, Quaternion.identity);
StartCoroutine (StartSpawning ());
}
}
PumkinScript
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PumkinScript : MonoBehaviour {
private int count;
public Text countText;
void Satrt(){
count = 0;
setCountText ();
}
void FixedUpdate ()
{
if (transform.position.y < -3f)
{
count = count + 1;
setCountText ();
Destroy (gameObject);
}
}
void OnCollisionEnter(Collision target)
{
if (target.gameObject.tag == "Golem") {
Destroy (target.gameObject);
Time.timeScale = 0f;
}
}
void setCountText()
{
countText.text = "Count : " + count.ToString ();
}
}
My errors are The referenced script on this Behaviour (Game Object 'Pumkin') is missing! UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion) c__Iterator0:MoveNext() (at Assets/Script/Spawner.cs:16) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
NullReferenceException: Object reference not set to an instance of an object PumkinScript.setCountText () (at Assets/Script/PumkinScript.cs:37) PumkinScript.FixedUpdate () (at Assets/Script/PumkinScript.cs:21)
This error means
Object reference not set to an instance of an object PumkinScript.setCountText ()
If you don't drag the UI for your setCountText() into the component then it's a Null Reference Exception.
Your answer
Follow this Question
Related Questions
making browse button 0 Answers
The application requires Unity3d permission 0 Answers
Problem with Json and Android ! 0 Answers
Script updates prefab text 0 Answers