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 /
  • Help Room /
avatar image
0
Question by ecequalsm2 · Apr 13, 2016 at 11:25 AM · enummanageraccessing from any scriptspawning-enemieswave

How to call an enum from another script with an enum

I'm having a little trouble trying to put these two scripts together for my enemy wave spawner. Can anyone help me with properly calling this enum? I've been going through this all day. Thanks in advance!

 **First Script:**
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public enum SpawnState {Spawning, Waiting, Counting};
 
 public static class WGen : MonoBehaviour {
 
 
 
     [System.Serializable]
  public class Wave
  {
      public string name;
      public GameObject enemy;
      public int spawnCount;
      public float wDelay;
      public Transform[] spawnPoints; // where enemy will spawn
      
  }
   
  public Wave[] waves;
  private int nextWave = 0;
  
  public float timeBetweenWaves = 5f;
  public float waveCountdown;
  //search for enemies on screen to happen only once per second, to avoid overtaxing the game
  private float searchCountdown = 1f; 
  
  private SpawnState state = SpawnState.Counting;
  
  
      void Start()
      {
        waveCountdown = timeBetweenWaves;  
        
      }
      
      void Update()
      {
          if(state == SpawnState.Waiting)
          {
              // -2- Them if we are waiting, we should check if all enemies are destroyed by player or Game 
              if(!EnemyIsAlive())
              {
                  // -3- If none are alive, then we begin a new round
                  Debug.Log("Wave Completed!");
                  return;
              }
              else
              {
                  // -4- if enemies are still alive, then return
                  return;
              }
          }
          //1) Check to see if we're at 0 in our countdown yet (should not be) **
          if (waveCountdown <= 0)
          {
              //2) once we reach 0, we'll check if we're already spawning **
              if (state != SpawnState.Spawning)
              {
                  //3) if we're not already spawning then we will start spawning wave **
                  StartCoroutine(SpawnWave (waves[nextWave]));
              }
              else
              {
                  {
                      //4) So we'll subtract from our countDown until we reach 0 **
                      waveCountdown -= Time.deltaTime;
                  }
              }
          }
      }
          
          bool EnemyIsAlive()
          {
              //Countdown from preset time of 1 second to 0 seconds, before starting 
              searchCountdown -= Time.deltaTime;
              if(searchCountdown >= 0f)
              {
                  //reset searchCountdown
                  searchCountdown = 1f;
                  //Check to see if there are any enemies left with on the screen by using their tag as a reference
              if(GameObject.FindGameObjectWithTag("Enemy") == null)
                 {
                  //if there are none, return false
                  return false;
                 }
              }
              
              //otherwise, return true
              return true;
          }
          
          IEnumerator SpawnWave(Wave _wave)
          {
              Debug.Log("Spawning Wave: " + _wave.name);
              
              //5) That will put us in our Spawning State **
              state = SpawnState.Spawning;
              
              //7) Loop through the number of enemies that we want to spawn **
              
              for(int i = 0; i < _wave.spawnCount; i++)
              {
                  SpawnEnemy(_wave.enemy);
                  
                  //8) Then, wait a certain amount of seconds before looping through the IEnumerator again  **
                  yield return new WaitForSeconds(_wave.wDelay);
              }
              // -1- After Spawning, set the SpawnState to Waiting
              state = SpawnState.Waiting;
              
              yield break;
              
          }
          //6) For each enemy that we want to spawn, we call the SpawnEnemy method to instantiate an enemy **
          void SpawnEnemy(GameObject _enemy)
          {
              
              //Spawn Enemy
              Debug.Log("Spawning Enemy: " + _enemy.name);
              
              // Only pick a new spawn point once per wave
            Instantiate(_enemy, transform.position, transform.rotation);
          }
  }

 **Second script (the one I'm having trouble with):**
 
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public static class GameManager : MonoBehaviour {
 
 //Reference to game objects
 public GameObject playButton;
 public GameObject playerShip;
 public GameObject enemySpawner; // reference to enemy spawner
 public SpawnState spawnState; //reference to SpawnState enum in WGEN
 public GameObject GameOverGO;
 public GameObject scoreUITextGO; // reference to score UI
 
 
 public enum GameManagerState
 {
     
     Opening,
     Gameplay,
     Intermission,
     GameOver,
 }
 
 GameManagerState GMState;
 //public enum.SpawnState spawnState { get; set;}
     // Use this for initialization
     void Start () {
       
     GMState = GameManagerState.Opening;
     //trying to access SpawnState enum from other script
     WGen.SpawnState spawnW = WGen.SpawnState.Waiting;
     }
     
     // Update is called once per frame
     void UpdateGameManagerState () {
      switch(GMState)
      {
          case GameManagerState.Opening:
          
          
          //set play button to visible (active)
          playButton.SetActive(true);
          
          break;
          case GameManagerState.Gameplay:
          
          //Reset the score
          scoreUITextGO.GetComponent<GameScore>().Score = 0;
          
          
          //hide play button on game play state
          playButton.SetActive(false);
          
          //Set player shooting to Invoke Repeating
          playerShip.GetComponent<Player>().PlayerFiring();
          
          //set player to visible (active) and init the player lives
          playerShip.GetComponent<Player>().Init();
          
          //Trying to start enemy spawner, but I'm having trouble here
 //error says: Only assignment, call, increment, decrement, and new object expressions can be used as a //statement
          spawnW;
          
          break;
          
          case GameManagerState.GameOver:
          
          //TODO: Stop enemy spawner, once I figure out how to start it
          
          
          
          
          //change game manager state to Opening state after 5 second delay
          Invoke("ChangeToOpeningState", 5f);
          break;
      }
     }
     
     public void SetGameManagerState(GameManagerState state)
     {
         GMState = state;
         UpdateGameManagerState();
     }
     
     public void StartGamePlay(){
         GMState = GameManagerState.Gameplay;
         UpdateGameManagerState();
     }
     
     public void ChangeToOpeningState()
     {
         SetGameManagerState(GameManagerState.Opening);
     }
 }


I've looked through multiple answers for questions that were similar to mine, but either my case is a little different or I'm just not getting it. Any help is greatly appreciated. Been trying to work this out for a month now. Thanks again! Milan

Comment
Add comment · Show 4
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
avatar image Polymo · Apr 13, 2016 at 11:45 AM 0
Share

Your example is missing the crucial line or i can't find it because the formatting is a little random. "//error says: Only assignment, call, increment, decrement, and new object expressions can be used as a //statement spawnW;" what is "spawnW;"? If it is the variable, then just set it to something like spawnW = SpawnState.Spawning;

avatar image ecequalsm2 Polymo · Apr 13, 2016 at 03:47 PM 0
Share

I was trying to declare spawnW as my variable. I tried something similar in this line:

//trying to access SpawnState enum from other script WGen.SpawnState spawnW = WGen.SpawnState.Waiting;

So, I should just take out the WGen part and make it:

spawnW = SpawnState.Waiting;

is that correct?

avatar image Polymo ecequalsm2 · Apr 14, 2016 at 06:45 AM 0
Share

It looks like your script is checking if there are remaining spawns before it starts a new wave continuously, so i guess you would either have to destroy existing enemies, or just i guess what you want is set the state variable: just make this "private SpawnState state = SpawnState.Counting;" public and do [referencetothespawnercomponent].state = SpawnState.Spawning. But keep in $$anonymous$$d that you're overwriting the scripts checks that way, so depending on your use cases it could break things.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Blue-Cut · Apr 14, 2016 at 05:46 PM

Hello,

Your code is very long so I am a bit confused, but to call an enum from 1 script in another, you need to write the name of the class that contains the enum.

 // In script 1
 public class ClassEnum
 {
            public enum MyEnum { TYPE1, TYPE2, TYPE3 }
 }
 
 // In script 2
 public class ClassCaller
 {
        public void SomeFunction()
        {
               ClassEnum.MyEnum var = ClassEnum.MyEnum.TYPE1
        }
 }


But I saw something like this in your code, so I am not sure where your problem is. Can you please edit your question to make your code cleaner and more readable ? By selecting relevant extracts and be carefull with the comment typo.

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
avatar image
-2

Answer by N1warhead · Apr 14, 2016 at 11:13 AM

I'm not even gonna read all your code. As I just had this issue earlier today, I found a work around as I couldn't figure it out either.

All I did was create a new Method (In C#).

 public void WhatEver(string newEnumValue){
 if(newEnumValue == "WhatEver"){
 enum = Enum.Whatever;
 }
 }

So pretty much what's going on here, we send a string value from the other script. And if the string value = whatever you want, then run the Enum as you would inside the same script already.

Comment
Add comment · Show 3 · 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
avatar image Polymo · Apr 14, 2016 at 04:05 PM 0
Share

Dont do that (this is too error prone). If you want to set an enum just pass the enum value.
$$anonymous$$yEnum {None,Something} public void Set$$anonymous$$yEnum($$anonymous$$yEnum enumValue){ myEnumField = enumValue; } or have an argument-less method, that just sets it to the required value.

avatar image ecequalsm2 Polymo · Apr 14, 2016 at 05:34 PM 0
Share

@ValooFX Thanks again! I really appreciate your help here!

avatar image ecequalsm2 · Apr 14, 2016 at 05:35 PM 0
Share

@N1warhead I really appreciate the reply. Every little bit helps.

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

6 People are following this question.

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

Related Questions

SceneManager: how can I find GameObject in another scene? 1 Answer

Where can i find package manger?,I cannot find package manager in my unity? 1 Answer

Damage manager (ThirdPersonShooter) 1 Answer

color is not changing in enum code 0 Answers

Using Direction enum to move an enemy from off screen 0 Answers


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