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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem at spawning script

Discussion in 'Scripting' started by Viktor-Theodor, Apr 15, 2015.

?

How to repair this?

  1. I have no idea

    1 vote(s)
    100.0%
  2. Something is not correct

    0 vote(s)
    0.0%
  1. Viktor-Theodor

    Viktor-Theodor

    Joined:
    Mar 23, 2015
    Posts:
    2
    I have a problem with this spawning script and I don't know how to repair it.
    using UnityEngine;
    using System.Collections;

    public class Spawn : MonoBehaviour {

    public GameObject[] eggs;
    private float time;
    public float timelimit=0.5f;
    bool spot=false;
    void Start () {

    }

    void Update () {

    time += Time.deltaTime;
    if (time >= timelimit) {
    time = 0;
    StartCoroutine(Spwn());
    }
    }
    public void Spot(bool x)
    {
    spot = x;
    }
    IEnumerator Spwn()
    {
    while(!spot)
    {
    spot=true;
    Instantiate(Random.Range(0f,11f), new Vector3(Random.Range (-37.31105f,51.35894f), 0f,Random.Range (-115.5185f,-189.02f)), Quaternion.identity) as GameObject;
    yield return null;
    }
    }
    }
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    use the Code Tags
    Also you should explain what your problem is and post any error messages that unity gives you in the console.
     
  3. Viktor-Theodor

    Viktor-Theodor

    Joined:
    Mar 23, 2015
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour {
    5.  
    6.     public GameObject[] eggs;
    7.     private float time;
    8.     public float timelimit=0.5f;
    9.     bool spot=false;
    10.     void Start () {
    11.        
    12.     }
    13.  
    14.     void Update () {
    15.    
    16.         time += Time.deltaTime;
    17.         if (time >= timelimit) {
    18.             time = 0;
    19.             StartCoroutine(Spwn());
    20.         }
    21.     }
    22.     public void Spot(bool x)
    23.     {
    24.         spot = x;
    25.     }
    26.     IEnumerator Spwn()
    27.     {
    28.         while(!spot)
    29.         {
    30.             spot=true;
    31.             Instantiate(Random.Range(0f,11f), new Vector3(Random.Range (-37.31105f,51.35894f), 0f,Random.Range (-115.5185f,-189.02f)), Quaternion.identity) as GameObject;
    32.             yield return null;
    33.         }
    34.     }
    35. }
    36.  
    Sorry for the post, i didn't know about the code tag and the error is: Assets/Spawn.cs(31,182): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement, at the 31st line
     
  4. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    You are trying to Instantiate a float value... should be a gameobject

    Code (csharp):
    1.  
    2. Instantiate(prefabName, newVector3(Random.Range(-37.31105f,51.35894f), 0f,Random.Range(-115.5185f,-189.02f)), Quaternion.identity)asGameObject;
    3.