[英]How to increase score in Unity 3D OnCollisionEnter
I'm stuck with ac# code that should be easy to solve at first instance but I'm not able to get it work. 我受困于一开始应该很容易解决的ac#代码,但是我无法使其正常工作。 I'm making a videogame in Unity3D and I want to make a function that increases the score once the object Collides with a wall. 我正在Unity3D中制作视频游戏,并且想创建一个函数,一旦物体与墙碰撞,该函数就可以增加得分。
I have 2 script for that: 我有2个脚本:
CODE: 码:
Puntuacion.cs Puntuacion.cs
using UnityEngine;
using System.Collections;
public class Puntuacion : MonoBehaviour
{
public int puntuacionActual=0;
public int maximaPuntuacion = 14;
public Transform gamePointsTextActual; // Texto Marcador Actual
public Transform gamePointsTextMaxima; // Texto Marcador Maxima
void Start ()
{
gamePointsTextActual=GameObject.Find("Texto 3D - ScoreActual").transform;
gamePointsTextMaxima=GameObject.Find("Texto 3D - ScoreMaxima").transform;
}
void Update ()
{
gamePointsTextActual.GetComponent<TextMesh>().text=this.puntuacionActual.ToString("D3");
gamePointsTextMaxima.GetComponent<TextMesh>().text=this.maximaPuntuacion.ToString("D3");
if(puntuacionActual>maximaPuntuacion)
{
maximaPuntuacion = puntuacionActual;
gamePointsTextActual.GetComponent<TextMesh>().text=this.maximaPuntuacion.ToString("D3");
}
}
public void incrementarPuntuacion()
{
puntuacionActual = puntuacionActual+1;
Debug.Log("Puntuacion Actual :"+puntuacionActual);
if(puntuacionActual>maximaPuntuacion)
{
maximaPuntuacion = puntuacionActual;
}
}
}
Collisions.cs Collisions.cs
using UnityEngine;
using System.Collections;
public class Collisions : MonoBehaviour
{
public Puntuacion puntuacion = new Puntuacion();
public void OnCollisionEnter(Collision obj_collision )
{
if(obj_collision.gameObject.name == "HitScore")
{
Debug.Log("Collision");
puntuacion.incrementarPuntuacion();
Destroy(gameObject,2.0F);
}
if(obj_collision.gameObject.name == "Plane")
{
Debug.Log("Collision Plano");
Destroy(gameObject,1.0F);
}
}
}
Both Script are attached to the same GameObject but I get this error: 两个脚本都附加到相同的GameObject,但出现此错误:
NullReferenceException: Object reference not set to an instance of an object
Collisions.OnCollisionEnter (UnityEngine.Collision obj_collision
On the other hand, the code does not increase the score, always increases 1 and the actual score stays in 1. I guess that is because everytime there is a Collision on the game, I create a new instance of Puntuacion.cs including the values puntuacionActual=0 and maximaPuntuacion = 14; 另一方面,代码不会增加分数,总会增加1且实际分数保持为1。我想这是因为每次游戏上发生碰撞时,我都会创建一个包含值的新Puntuacion.cs实例。 puntuacionActual = 0,maximumPuntuacion = 14; and increases the value from that puntuacionActual from 0 to 1 in each collision. 并在每次碰撞中将puntuacionActual的值从0增加到1。
What I would like to achieve is to increase the score value (+1) whenever there is a collision on the system the wall, just that.I would like to save that value for the next collision. 我想要实现的是在系统墙壁发生碰撞时增加得分值(+1),我想为下次碰撞保存该值。
I will be very grateful to whoever helps me with this doubt 我将非常感谢任何帮助我解决这个问题的人
Best regards 最好的祝福
Yes I think you are indeed right, your score always gets reset because you create an instance of new Punctuation
in Collisions
script. 是的,我认为您确实是对的,您的分数总是会重置,因为您在Collisions
脚本中创建了新Punctuation
的实例。 Try using GetComponent
instead: 尝试改用GetComponent
:
Punctuation punctuationScript= gameObject.getComponent<Punctuation>();
Then you can access the public variables or methods just like this: 然后,您可以像下面这样访问公共变量或方法:
punctuationScript.incrementarPunctuation();
This way no new script will be created, instead it will just get the script from that gameObject. 这样,将不会创建新脚本,而只会从该gameObject获取脚本。
EDIT: 编辑:
I just copied your code into my project and I get the same error as you when I used your code: 我只是将您的代码复制到我的项目中,所以在使用代码时会收到与您相同的错误:
Collisions.cs Collisions.cs
public Puntuacion puntuacion = new Puntuacion();
public void OnCollisionEnter(Collision obj_collision)
{
if(obj_collision.gameObject.name == "HitScore")
{
Debug.Log("Collision");
puntuacion.incrementarPuntuacion();
Destroy(gameObject,2.0F);
}
}
However after I tried using the code I gave you, everything went fine and the score even got incremented correctly: 但是,当我尝试使用提供给您的代码后,一切都很好,并且分数甚至正确地提高了:
public void OnCollisionEnter(Collision obj_collision)
{
if(obj_collision.gameObject.name == "HitScore")
{
Debug.Log("Collision");
Puntuacion puntuacionScript = gameObject.GetComponent<Puntuacion>();
puntuacionScript.incrementarPuntuacion();
Destroy(gameObject,2.0F);
}
}
Are you sure you have made it like this? 您确定已经做到了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.