用于处理基本立方体上的点击的原始脚本
using UnityEngine;
using System.Collections;
public class TileClickController : MonoBehaviour
{
Ray ray;
RaycastHit hit;
[SerializeField] private int health = 5;
[SerializeField] private int maxHealth = 0;
//status
//0 - closed
//1 - opened
[SerializeField] private int status = 0;
void Start()
{
this.maxHealth = this.health;
}
// Update is called once per frame
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(Input.GetMouseButtonDown(0))
{
if( this.health <= 0 )
{
this.status = 1;
}
else
{
this.health--;
}
print(hit.collider.name);
}
}
}
}
该脚本与预制件相关联。舞台上放置了两个或多个预制件实例。
问题:
- 当您单击任何对象时,预制件的所有实例中的变量值都会发生变化。
- 单击通知在控制台中显示的次数与放置的副本的次数相同(我附上了屏幕截图)。在这种情况下,每个实例都有自己唯一的名称,但“单击”对象的名称显示在控制台中。
基本上脚本中缺少这一点
