How do i fix this error?
How to fix this error: NullReferenceException: Object reference not set to an instance of an object NewBehaviourScript2.Update () (at Assets/Scripts/NewBehaviourScript2.cs:17)
Scripts: NewBehaviourScript:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
Transform hareket;
public float Hiz = 3.0f;
// Başlangıçta çalıştırılır.
void Start () {
hareket = transform;
hareket.position = new Vector2 (224, 312);
}
// Her framede bir kez çalıştırılır.
void Update () {
hareket.Translate(Input.GetAxis("Horizontal")*Vector2.right*Hiz*Time.deltaTime);
hareket.Translate(Input.GetAxis("Vertical")*Vector2.up*Hiz*Time.deltaTime);
if (hareket.position.x > 237) {
hareket.position = new Vector2 (211, 321);
}
if (hareket.position.x < 211) {
hareket.position = new Vector2 (237, 321);
}
}
}
NewBehaviourScript1:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour {
public int score=0;
// Başlangıçta çalışır.
void Start () {
}
// Her framede çağırılır.
void Update () {
}
// Belirli bir nesneye deydiğinde çalışır.
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.name == "1Score") {
score++;
Destroy (coll.gameObject);
Debug.Log (score);
}
}
}
NewBehaviourScript2:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript2 : MonoBehaviour {
Transform hareket2;
public NewBehaviourScript1 kod;
// Use this for initialization
void Start () {
hareket2 = transform;
hareket2.position = new Vector2 (210,320);
}
// Update is called once per frame
void Update () {
if (kod.score == 1) {
hareket2.position = new Vector2 (213, 320);
}
}
}
I tried to change a variable in NewBehaviourScript1 from NewBehaviourScript2.
you need to set your variable kod
to a reference of NewBehaviourScript1
you can do that by finding the game object it's attached to and using GetComponent()
- check the documentation for finding game objects and using GetComponent()
Your answer
Follow this Question
Related Questions
How do I check if the current PieceType is a King? 1 Answer
The body of '' cannot be an iterator block because 'void' is not an iterator interface type??? 2 Answers
Coding Errors 1 Answer
A namespace cannot directly contain mambers such as fields or methods... 2 Answers
Flashlight script help! 2 Answers