Search Unity

Spawning Cubes?

Discussion in 'Scripting' started by MichaelHotte, Feb 28, 2015.

  1. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    I'm having trouble spawning cubes in my game. The game runs but I get the following error and my cubes will not spawn:

    "Trying to Invoke method: CubeManager1.Spawn couldn't be called."

    This'll pop up every 3 seconds because my spawnTime is equal to 3 seconds and not spawn the cube like I want it to. Any suggestions? My code is below, not really sure what is wrong with it. Thanks in advance.

    using UnityEngine;

    public class CubeManager1 : MonoBehaviour
    {
    public GameObject enemy; // The enemy prefab to be spawned.
    public float spawnTime = 3f; // How long between each spawn.
    public Vector3 spawnLocation = new Vector3(0,2,0);

    void Start ()
    {
    InvokeRepeating ("Spawn", spawnTime, spawnTime);
    GameObject SpawnLocation = (GameObject)Instantiate (enemy, spawnLocation, Quaternion.identity);
    }
    }

    Also the cube will only spawn one time but none after and then the error pops up every 3 seconds.
     
    Last edited: Feb 28, 2015
  2. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Please read this and edit your post so your code is readable.
    Also have a look at the InvokeRepeating documentation for proper usage. You're trying to invoke a function (in this case a function named Spawn) that doesn't exist. Move your spawn code to a function named Spawn and see if that works.
     
    MaximilianPs likes this.
  3. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    It works now! I adjusted the code like you said and it spawns like it is supposed to now!

    Also i'll start attaching my code instead of copying and pasting now, my bad
     
  4. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I'm currently doing the same thing , how did you adjust your code?
     
  5. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    If this is what you're looking for, this'll create a cube located at the spawnLocation with a delay of 3 seconds then create another cube every 2 seconds.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CubeManager1 : MonoBehaviour
    4. {
    5.     public GameObject enemy;              
    6.     public float spawnTimeDelay = 3f;
    7.     public float spawnTimeInGame = 2f;
    8.     public Vector3 spawnLocation = new Vector3(0,2,0);
    9.  
    10.  
    11.  
    12.     void Spawn () {
    13.         GameObject SpawnLocation = (GameObject)Instantiate (enemy, spawnLocation, Quaternion.identity);
    14.     }
    15.  
    16.     void Start ()
    17.     {
    18.         // Call the Spawn function after a delay of the spawnTimeDelay and then continue to call after a certain amount of time.
    19.         InvokeRepeating ("Spawn", spawnTimeDelay, spawnTimeInGame);
    20.     }
    21. }
     
  6. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Exactly what I was looking for, Thanks man.:):);)
     
  7. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Started coding but Unity gave me the following error. Spawn.Spawn member names cannot be the same as their enclosing types. Here is my code if it helps.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour {
    5.     public GameObject Cube;
    6.     public float spawnTimeDelay = 3f;
    7.     public float spawnTimeInGame = 2f;
    8.     public Vector3 spawnLocation = SpawnLocation;
    9.  
    10.     void Spawn ()
    11.     {
    12.         GameObject SpawnLocation = (GameObject) Instantiate ( Cube, spawnLocation, Quaternion.identity);
    13.     }
    14.  
    15.        
    16.        
    17.     // Use this for initialization
    18.     void Start  ()
    19.     {
    20.         InvokeRepeating ("Spawn", spawnTimeDelay, spawnTimeInGame);
    21.     }
    22. }
    23.  
    24.  
     
  8. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    A function inside a class that shares the same name as that class becomes that classes constructor. Since your class's name is Spawn and your function's name is Spawn C# treats the Spawn function as a constructor for the class. Simply change the name of the class (e.g. to Spawner or SpawnManager) or change the name of the function (e.g. to SpawnCube etc.).
     
    MichaelHotte likes this.
  9. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
  10. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Dang it ! now I can't test it because Unity says the script and class name don't match. I was trying to rename the script , but it doesn't seem you can do it.
     
  11. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    rename the file and the class so their names match ?
     
  12. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    yup , I can't do that.