Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alex_Edeve · Apr 19, 2019 at 03:57 PM · randomspawning problemsrandom spawn

Enemy spawn logic not working

I am making a top down 2D game. I have 12 predefined spawn points for enemies, represented by a sprite on screen. on this sprite I have the following script.

 using System.Collections;
 using System;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LogicSpawn : MonoBehaviour
 {
     //RNG
     System.Random RNG = new System.Random();
     public string Direction;
     public Transform DragonPrefabGreen;
     public Transform DragonPrefabGold;
 

 //update once per frame
 void update()
 {
     //conects this script to the drgon tracker script
     DragonTracker dt = GameObject.Find("_DragonManager").GetComponent<DragonTracker>();  
     Timer T2 = GameObject.Find("TimerText").GetComponent<Timer>();

     //spawn logic variables
     int RSpawn = RNG.Next(0, 2);
     print(RSpawn);
     print("RSpawn");
     int DragonType = RNG.Next(0, 101);
     bool GoldDragonInit = dt.GoldDragonInit; 
     int DragonCount = dt.DragonCount;
     int Difficulty = dt.Difficulty;

     //spawning logic
     if ((RSpawn == 1) && (DragonCount < Difficulty))
     {
         if (DragonType > 99)
         {
             //summon regular dragon
             Instantiate(DragonPrefabGreen, transform.position, transform.rotation);
         }
         if ((DragonType == 100)&&(GoldDragonInit==false))
         {
             //Sumon gold dragon
             Instantiate(DragonPrefabGold, transform.position, transform.rotation);
         }
     }
 }

}

I have a parent game object that all of these sprits are placed beneath in the hierarchy. this game object has a script that contains all of the spawning variables. This keeps track of the current number of enemies in the game, if a special enemy is in play, and to set the difficulty level.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DragonTracker : MonoBehaviour
 {
     // is gold dragon in play?
     public bool GoldDragonInit = false;
     // curently active dragons
     public int DragonCount = 0;
     // defalts to 5
     public int Difficulty = 5;
 }

the problem I am having is the spawning does not appear to work. the only thing I can think of is the random number generator is not working, which Is why I put the print (RSpawn);. nothing appears in the console so it appears that this may be the case but I don't know why it doesn't work.

any help is greatly appreciated.

I will also need to change the variables in the variable tracking script when different events happen, such as an enemy spawns. if anyone could tell me how to do that while I am here that would be great.

thanks is advanced.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Apr 19, 2019 at 05:00 PM

The function Update is misspelled as update. But you should also improve your code, moving part of it to Start:

      public class LogicSpawn : MonoBehaviour
      {
          //RNG
          System.Random RNG = new System.Random();
          public string Direction;
          public Transform DragonPrefabGreen;
          public Transform DragonPrefabGold;
 
          // move these variables outside any function
          DragonTracker dt;
          Timer T2;
     
      
       void Start(){
 
          //connects this script to the dragon tracker script
          dt = GameObject.Find("_DragonManager").GetComponent<DragonTracker>();  
          T2 = GameObject.Find("TimerText").GetComponent<Timer>();
       }
     
       void Update(){
          //spawn logic variables
          int RSpawn = RNG.Next(0, 2);
          print(RSpawn);
          print("RSpawn");
          int DragonType = RNG.Next(0, 101);
          bool GoldDragonInit = dt.GoldDragonInit; 
          int DragonCount = dt.DragonCount;
          int Difficulty = dt.Difficulty;
          //spawning logic
          if ((RSpawn == 1) && (DragonCount < Difficulty))
          {
              if (DragonType > 99)
              {
                  //summon regular dragon
                  Instantiate(DragonPrefabGreen, transform.position, transform.rotation);
              }
              if ((DragonType == 100)&&(GoldDragonInit==false))
              {
                  //Sumon gold dragon
                  Instantiate(DragonPrefabGold, transform.position, transform.rotation);
              }
          }
      }
   }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How would 1 instantiate a cube on a random position on screen , ortographic camera(2d game)? 1 Answer

Multiple spawn location with random game object in random time spawn c# 1 Answer

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

[C#] How do I randomly pick between 3 specific numbers? 1 Answer

Problem with instantiating the object in untiy at certain time intervals 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges