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

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

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:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PipeSpawnScript : MonoBehaviour
  5. {
  6. public GameObject pipe;
  7. public float spawnRate = 2;
  8. private float timer = 0;
  9. // 在第一帧之前调用
  10. void Start()
  11. {
  12. SpawnPipe();
  13. }
  14. // 每帧调用一次
  15. void Update()
  16. {
  17. if (timer < spawnRate)
  18. {
  19. timer = timer + Time.deltaTime;
  20. }
  21. else
  22. {
  23. SpawnPipe();
  24. timer = 0;
  25. }
  26. }
  27. void SpawnPipe()
  28. {
  29. Instantiate(pipe, transform.position, transform.rotation);
  30. }
  31. }

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:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PipeSpawnScript : MonoBehaviour
  5. {
  6. public GameObject pipe;
  7. public float spawnRate = 2;
  8. private float timer = 0;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. void spawnPipe();
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. if (timer &lt; spawnRate)
  18. {
  19. timer = timer + Time.deltaTime;
  20. }
  21. else
  22. {
  23. void spawnPipe();
  24. timer = 0;
  25. }
  26. }
  27. void spawnPipe()
  28. {
  29. Instantiate(pipe, transform.position, transform.rotation);
  30. }
  31. }

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:

  1. void Start()
  2. {
  3. void spawnPipe() { }
  4. }

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

This calls an existing method:

  1. void Start()
  2. {
  3. spawnPipe();
  4. }

Remove the void keyword!

英文:

This declares a local function which is never called:

  1. void Start()
  2. {
  3. void spawnPipe() { }
  4. }

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

This calls an existing method:

  1. void Start()
  2. {
  3. spawnPipe();
  4. }

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:

确定