Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

coroutine script error

Discussion in 'Scripting' started by castoldigabriele, Feb 20, 2018.

  1. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    i try to do this script but there is an error, how can i fix it??


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;

    4. public class SpawnPlatform : MonoBehaviour {

    5. public Vector3 SpawnPosition;
    6. public float MaxWidth;
    7. public float MinWidth;
    8. public float MaxLenth;
    9. public float MinLenth;
    10. public float MoveSpeed;
    11. public float ScaleSpeed;
    12. private GameObject cube;


    13. IEnumerator Start ()
    14. {
    15. yield return StartCoroutine (Spawn ());
    16. }


    17. void Start () {
    18. cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    19. cube.transform.position = SpawnPosition;
    20. cube.transform.localScale = RandomLenth();
    21. }

    22. void Update()
    23. {
    24. cube.transform.position += -Vector3.right * Time.deltaTime * MoveSpeed;

    25. cube.transform.localScale += new Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime) * ScaleSpeed;

    26. }

    27. Vector3 RandomLenth()
    28. {
    29. return new Vector3(Random.Range(MinLenth, MaxLenth),1,1);
    30. }

    31. IEnumerator Spawn()
    32. {
    33. while (true) {
    34. yield return new WaitForSeconds (Random.Range (1, 5));
    35. }
    36. }

    37. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    You can have only one Start method. The void one. For starters, also post your error message(s) and put the code in code tags here in your forum post.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Start (and some others of Unity's so called "messages") can actually be declared having an IEnumerator as return value. The engine will be aware of it and will start it as a coroutine instead.

    Anyway it's true, he has to choose the one or the other.
     
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Since this doesn't seem to have any dependencies on other objects, the easiest fix would be to rename the void Start() to Awake().

    I'm assuming you're aware the Spawn() function doesn't actually do anything useful, though.