我遇到了两个无法解决的问题。 修饰语“私人”对此项目无效。 和 声明了局部函数OnTriggerEnter2D,但从未使用过。谁能看一下并帮助我确定问题出在什么地方?
谢谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
private object collision;
public float _speed = 3.0f;
void Start()
{
}
// Update is called once per frame
void Update() {
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if (transform.position.y < (-5.71f))
{
float Randomx = Random.Range(-5, 5);
transform.position = new Vector3(Randomx, 5.71f, 0);
}
}
}
}
}
答案 0 :(得分:1)
在C#中,您可以在函数内部具有函数(有关Local Function的更多信息。要解决该错误,请执行以下任一操作:
(./mkMyTypeConstructor.dhall Text).some "foo"
内部拉出函数Update
或者这个:
private void Update() { }
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if (transform.position.y < (-5.71f))
{
float Randomx = Random.Range(-5, 5);
transform.position = new Vector3(Randomx, 5.71f, 0);
}
}
编辑:当然,如果在另一种方法中使用private void Update()
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if (transform.position.y < (-5.71f))
{
float Randomx = Random.Range(-5, 5);
transform.position = new Vector3(Randomx, 5.71f, 0);
}
}
}
,则Unity将无法找到它,因此代码将无法工作。