【发布时间】:2023-01-25 03:55:27
【问题描述】:
我正在尝试制作一种防止玩家发送垃圾邮件的方法空间钥匙。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerX : MonoBehaviour
{
public GameObject dogPrefab;
private float setTime = 2.0f;
private float timer = Time.time;
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (timer >= setTime)
{
SpawnDog();
}
}
void SpawnDog()
{
// On spacebar press, send dog
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
timer = 0;
}
}
}
这是来自 Unity 的错误声明:我认为这不是逻辑错误
UnityException:get_time 不允许从 MonoBehaviour 构造函数(或实例字段初始值设定项)中调用,而是在 Awake 或 Start 中调用它。从游戏对象“Player”上的 MonoBehaviour“PlayerControllerX”调用。
【问题讨论】: