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

My Instantiation Script Doesn't Instantiate

Discussion in 'Scripting' started by wwyliee, Apr 26, 2020.

  1. wwyliee

    wwyliee

    Joined:
    Jan 31, 2020
    Posts:
    7
    My Script will spawn one enemy at the beginning, but the while loops don't seem to do anything, I'm relatively new to Unity, any help would be much appreciated. Thanks
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Security.Cryptography;
    5. using Unity.Mathematics;
    6. using UnityEngine;
    7.  
    8. public class SpawnEnemys : MonoBehaviour
    9. {
    10.    
    11.     //Declare Varibales
    12.     public int spawntimemax, spawntimemin;
    13.    
    14.     //Makes a new random number every time its used
    15.     System.Random rnd = new System.Random();
    16.     public float WaveNum;
    17.     public float WaveLength;
    18.     public GameObject enemy;
    19.    
    20.     //declaring a different script so I can use it's variables
    21.     public EnemyDies script;
    22.     void Start()
    23.     {
    24.         //Creates One enemy at the very start of the game
    25.         Spawn();
    26.         //accessing the script I was declaring on line 21
    27.         script = enemy.GetComponent<EnemyDies>();
    28.        
    29.         //Giving WaveNum and WaveLength values, and having WaveNum directly affect the SpawnTimes
    30.         WaveNum = 1;
    31.         WaveLength = 30;
    32.         spawntimemin = Convert.ToInt32(Mathf.Sqrt(WaveNum) * 5);
    33.         spawntimemax = Convert.ToInt32(Mathf.Sqrt(WaveNum) * 10);
    34.        
    35.         //making loops that run until you reach 100 points
    36.         while (script.points == 100)
    37.         {
    38.             //running the Spawn function at a random time between the spawntime variables
    39.             Invoke("Spawn", rnd.Next(spawntimemin, spawntimemax));
    40.             while (script.points == 100)
    41.             {
    42.                 //Making WaveNum larger after "WaveLength" Number of seconds have gone by, then making WaveLength Larger
    43.                 Invoke("IncrementWave", WaveLength);
    44.                 WaveLength *= 1.5f;
    45.             }
    46.         }
    47.     }
    48.    
    49.     //a method that Intantiates the GameObject called "enemy"
    50.     public void Spawn()
    51.     {
    52.         Instantiate(enemy);
    53.     }
    54.  
    55.     //a method that makes WaveNum 1 larger
    56.     public void IncrementWave()
    57.     {
    58.         WaveNum++;
    59.     }
    60. }
     
  2. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    Code (CSharp):
    1.         //making loops that run until you reach 100 points
    2.         while (script.points == 100)
    One of bugs is here "script.points == 100" condition is true with script.points being exactly 100.

    "until you reach 100 points" requires something like

    "script.points != 100" true if script.points differs from 100
    "script.points < 100" true if script.points is lower than 100
    "script.points > 100" true if script.points is greater than 100

    (which ones - depends on logic in your script)

    Or maybe comment is wrong, not condition.
    ---

    Also SpawnEnemys would benefit from renaming to SpawnEnemies (removes typo, not a bug or major problem but always an improvement)

    [there are likely other issues but maybe fixing broken condition will fix main bug - if not then please post an updated code]
     
  3. wwyliee

    wwyliee

    Joined:
    Jan 31, 2020
    Posts:
    7
    Thank you, I think think that was my problem
     
    matkoniecz likes this.