我刚开始学习Unity,似乎弄不明白,我做错了什么?

huangapple go评论68阅读模式
英文:

I just started learning unity and I can't seem to figure it out, what did I do wrong?

问题

I understand that you want the code to be translated. Here's the translated code without any additional content:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipeSpawnScript : MonoBehaviour
{
    public GameObject pipe;
    public float spawnRate = 2;
    private float timer = 0;

    // 在第一帧之前调用
    void Start()
    {
        SpawnPipe();
    }

    // 每帧调用一次
    void Update()
    {
        if (timer < spawnRate)
        {
            timer = timer + Time.deltaTime;
        }
        else
        {
            SpawnPipe();
            timer = 0;
        }
    }

    void SpawnPipe()
    {
        Instantiate(pipe, transform.position, transform.rotation);
    }
}

If you have any more code to translate or need further assistance, please let me know.

英文:

So, I was following a tutorial on unity and things were going fine but then I got the error 'Assets\PipeSpawnScript.cs(26,18): warning CS8321: The local function 'spawnPipe' is declared but never used' and I can't seem to figure it out what to do, I checked the code and compared it to the code in the tutorial but it was the exact same, here's the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipeSpawnScript : MonoBehaviour
{
    public GameObject pipe;
    public float spawnRate = 2;
    private float timer = 0;

    // Start is called before the first frame update
    void Start()
    {
        void spawnPipe();
    }

    // Update is called once per frame
    void Update()
    {
        if (timer &lt; spawnRate)
        {
            timer = timer + Time.deltaTime;
        }
        else
        {
            void spawnPipe();
            timer = 0;
        }
    }

    void spawnPipe()

    {
        Instantiate(pipe, transform.position, transform.rotation);
    }
}

Here is the link to the tutorial: https://www.youtube.com/watch?v=XtQMytORBmM

And I'm using version 2021.3.24f1

It should make a pipe as soon as the player starts the game (Note: I'm recreating Flappy Bird)

答案1

得分: 4

This declares a local function which is never called:

void Start()
{
    void spawnPipe() {   }
}

With void spawnPipe(); you will get even another error telling you that the local function needs a body.

This calls an existing method:

void Start()
{
    spawnPipe();
}

Remove the void keyword!

英文:

This declares a local function which is never called:

void Start()
{
    void spawnPipe() {   }
}

With void spawnPipe(); you will get even another error telling you that the local function needs a body.

This calls an existing method:

void Start()
{
    spawnPipe();
}

Remove the void keyword!

huangapple
  • 本文由 发表于 2023年5月13日 19:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定