【发布时间】:2022-01-25 08:57:10
【问题描述】:
我有两个脚本,一个是父母,一个是孩子。如果使用 Input.("Fire1"),父脚本会提供 On Mouseover 方法供子级使用。然后它应该禁用鼠标悬停的对撞机并得分。但是我遇到了一个错误,如果我点击一个带有子脚本的预制对撞机,它将禁用所有预制游戏对象。我该如何解决这个问题,以便我的子脚本一次只允许禁用一个预制件?
父脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectableParent : MonoBehaviour
{
protected GameManager gameManager;
// Start is called before the first frame update
void Awake()
{
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{
}
public void OnMouseOver()
{
if (Input.GetButtonDown("Fire1"))
{
this.gameObject.SetActive(false);
gameManager.UpdateScore(5);
}
}
}
子脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelOneJack : CollectableParent
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
OnMouseOver();
}
}
}
【问题讨论】:
-
请使用正确的标签!请注意,
unityscript是或更好的是曾经是一种 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,现在早已弃用了!
标签: c# unity3d parent-child onmouseover disable