Arkadaşlar basit bir oyun yaptım. Sağ sol tuşlarına basıp bir kutuyu hareket ettiriyorsun, yukarıdan elma düşüyor. Elma kutunun içerisine girerse 10 puan, yere değerse 5 puanın gitmesi gerekiyor. Bu normalde şartlarda çalışıyor ama textmesh componentini kullanınca elma toplayın puanım artıyor ama elma yere düşünce "NullReferenceException: Object reference not set to an instance of an object" hatası alıyorum.
Kodlarım iki classtan oluşuyor. Classlarım şu şekilde:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class karakterhareket : MonoBehaviour
{
public GameObject küp;
public float hiz;
public int puan;
public GameObject tablo;
// Start is called before the first frame update
void Start()
{
Vector3 konum = new Vector3(0,10,0);
GameObject yeniküp = Instantiate(küp,konum,Quaternion.identity);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left*hiz*Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right*hiz*Time.deltaTime);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "safir")
{
this.puan += 10;
puandurumu(puan);
float sayi = Random.Range(-10f, 10f);
collision.transform.position=new Vector3(sayi, 10f, 0);
}
}
public void puandurumu( int gelen)
{
Debug.Log(gelen);
tablo.GetComponent<TextMesh>().text= gelen.ToString();
}
public karakterhareket(int sondurum)
{
this.puan += sondurum;
puandurumu(puan);
}
}
----------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class başlangıç : MonoBehaviour
{
//public GameObject safir;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "safir")
{
collision.transform.position=new Vector3(0,10f,0);
int yenipuan=-5;
karakterhareket islem = new karakterhareket(yenipuan);
}
}
}
Bilen arkadaşlar hatanın nerede olduğunu söylerse çok mutlu olurum.
Kodlarım iki classtan oluşuyor. Classlarım şu şekilde:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class karakterhareket : MonoBehaviour
{
public GameObject küp;
public float hiz;
public int puan;
public GameObject tablo;
// Start is called before the first frame update
void Start()
{
Vector3 konum = new Vector3(0,10,0);
GameObject yeniküp = Instantiate(küp,konum,Quaternion.identity);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left*hiz*Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right*hiz*Time.deltaTime);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "safir")
{
this.puan += 10;
puandurumu(puan);
float sayi = Random.Range(-10f, 10f);
collision.transform.position=new Vector3(sayi, 10f, 0);
}
}
public void puandurumu( int gelen)
{
Debug.Log(gelen);
tablo.GetComponent<TextMesh>().text= gelen.ToString();
}
public karakterhareket(int sondurum)
{
this.puan += sondurum;
puandurumu(puan);
}
}
----------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class başlangıç : MonoBehaviour
{
//public GameObject safir;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "safir")
{
collision.transform.position=new Vector3(0,10f,0);
int yenipuan=-5;
karakterhareket islem = new karakterhareket(yenipuan);
}
}
}
Bilen arkadaşlar hatanın nerede olduğunu söylerse çok mutlu olurum.
Son düzenleyen: Moderatör: