[英]Having issues creating and destroying a script component in unity
我正在尝试制作它,以便您可以在 object 中添加和删除脚本,就像按下按钮的立方体一样。 这是我到目前为止所得到的,但它不断抛出一个错误,期待“;” & "{" 在同一空间 [第 24,71 行之后: if (Input.GetButtonDown("Button.Two")) GetComponent (Sticky))]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sticky2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
function Update(){
if (Input.GetButtonDown("Button.One"))
{
gameObject.AddComponent<Sticky>();
}
}
function Update() {
if (Input.GetButtonDown("Button.Two")) GetComponent (Sticky))
Destroy (GetComponent (Sticky));
}
}
function Update()
?
这是c#
不是unityscript
。
GetComponent (Sticky)
也应该是GetComponent<Sticky>()
if (Input.GetButtonDown("Button.Two")) GetComponent (Sticky))
应该怎么做?
通常,您的代码中缺少两个结束的}
,并且每个语句都以;
结尾在c#
它可能应该是例如
public class Sticky2 : MonoBehaviour
{
private void Update()
{
// Use GetComponent in order to check if such componnet already exists
if (Input.GetButtonDown("Button.One") && !GetComponent<Sticky>())
{
gameObject.AddComponent<Sticky>();
}
// Use TryGetComponent in order to check and get the component in one single go
if (Input.GetButtonDown("Button.Two") && TryGetComponent<Sticky>(out var sticky))
{
Destroy(sticky);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.