Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 iHunterKiller · Mar 04, 2014 at 11:57 PM · classspawningnoob

JS: Access var inside class

Im back with another problem in our spawning script. Were attempting to spawn units from the lists we created, but cant seem to figure out how to reference any vars inside these class.

Basically were trying to set it up so that everything works within the inspector.

Inside class composition we set the enemy types to be spawned, the position they spawn at and the amount to be spawned.

I'm not quite sure how to make it work though, the typical instantiate systems ive used before arnt working.

Also were not entirely sure how to go about making it work with the spawn count if anyone can help out with that too. For example if its set to 3 in the inspector, it should spawn 3 of those units at that spawn point.

Any help is much appreciated :)

 var enemies1 : GameObject;
 var enemies2 : GameObject;
 
 var point1 : GameObject;
 var point2 : GameObject;
 
 var amountOfWaves : WaveAmount[];


 
 class WaveAmount  
     {
         var name : String;
         var wavePushes : WavePushes[];
     }  
 class WavePushes
     {            
         var startTimeSinceLastPush : float;
         var enemyBuild : Composition[];          
     }   
 class Composition
     {
         var Enemy1 : EnemyList;
         var Spawn1 : SpawnPoint;
         var spawn1Count : int;
         
         var Enemy2 : EnemyList;  
         var Spawn2 : SpawnPoint;
         var spawn2Count : int;
     }


   
 function Start()
     {
         var spawnEnemy1 = Instantiate(Enemy1);
         spawnEnemy1.transform.position = new Vector3(Spawn1.transform.position, Quaternion.identity);
     }
 
 enum EnemyList
     {
         enemies1,
         enemies2
     }
 enum SpawnPoint
     {
         point1,
         point2 
     }
Comment
Add comment · Show 3
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 Jamora · Mar 05, 2014 at 12:17 AM 1
Share

You need to follow the references you've set up. You have no direct reference from the main class to the Composition class, ins$$anonymous$$d you have a reference from WavePushes to Composition . Following this logic all the way to amountOfWaves, the way you can access Composition from your outer class is

 amountOfWaves[waveNumber].wavePushes[pushNumber].enemyBuild[buildNumber].Enemy1;

I recommend 2 things:

1)make all variable names begin with lower case, this makes it impossible to mix them up with class names.

2) $$anonymous$$ake a getter in each class so ins$$anonymous$$d of the huge one-liner with ridiculous feature-envy you'd have, in your outermost class

 function GetEnemy1(wave :int ,push :int, composition : int){ 
 return amountOfWaves[wave].GetEnemy1(push,composition); 
 }

then, in WaveAmount, you'd have

 function GetEnemy1(push :int, composition : int){ 
 return wavePushes[push].GetEnemy1(composition); 
 }

etc. This way, if you ever make a change somewhere in this hierarchy, not everything will explode in your face at once.

With these getters set up, ins$$anonymous$$d of the huge one-liner, you'd have

 var spawnEnemy = Instantiate(GetEnemy1(1,3,2));
avatar image iHunterKiller Jamora · Mar 05, 2014 at 01:02 AM 0
Share

Oh wow, this is a little over my head, but i think ive more or less got the jist of it, but im a little unsure on which class is the outermost. I think the order is waveAmount, wavePushes, Composition (outermost being the last one?) but the way your code and comments are written im guessing its not even in that order lol.

Either way im getting errors, really unsure what to do with all these changes :/ Im quite dense when it comes to these things.

Among others, i think the main Error is the return not recognizing the amountOfWaves. Ive tried to fill it in to the best of my ability though. Likely something extremely obvious staring me in the face lol

 var enemies1 : GameObject;
 var enemies2 : GameObject;
 var point1 : GameObject;
 var point2 : GameObject;
 
 var amountOfWaves : waveAmount[];
 
 class waveAmount
 {
 var wavePushes : wavePushes[];
 
 function GetEnemy1(push :int, composition : int)
 
 { 
 return wavePushes[push].GetEnemy1(composition); 
 } 
  
 } 
 class wavePushes 
 { 
 var enemyBuild : Composition[]; 
 
 function GetEnemy1(wave :int ,push :int, composition : int)
 {
 // error on the amountOfWaves line below
 return amountOfWaves[wave].GetEnemy1(push,composition);
 }
 
 } 
 class Composition 
 { 
 var enemy1 : enemyList; 
 var spawn1 : spawnPoint; 
 var spawn1Count : int;
 
 var enemy2 : enemyList;
 var spawn2 : spawnPoint;
 var spawn2Count : int;
 }
 
 
 
 
 function Start()
 {
 var spawnEnemy = Instantiate(GetEnemy1(1,3,2));
 
 // spawnEnemy1.transform.position = new Vector3(Spawn1.transform.position, Quaternion.identity); 
 }
 
 enum enemyList
 {
 enemies1,
 enemies2
 }
 enum spawnPoint
 {
 point1,
 point2
 }
avatar image Jamora Jamora · Mar 05, 2014 at 01:21 AM 1
Share

There is only one level of nesting in this class.

 --------------------------------------
 |        outermost class              | 
 |               |                     |
 |    --------------------------       |
 |   |           |             |       |
 | WaveAmout  WavePushes   Composition |
 ---------------------------------------

Outermost class being the script itself.

 /* OUTER$$anonymous$$OST CLASS*/
 var enemies1 : GameObject;
 var enemies2 : GameObject;
      
 var point1 : GameObject;
 var point2 : GameObject;
      
 var amountOfWaves : WaveAmount[];
      
     
 function Start()
 {
     var spawnEnemy1;
     if(GetEnemy1(0,0,0) == EnemyList.enemies1)
         spawnEnemy1 = Instantiate(enemies1);
     //ETC.
     spawnEnemy1.transform.position = new Vector3(Spawn1.transform.position, Quaternion.identity);
     //You'll need the same getter-system for deter$$anonymous$$ing the correct spawn point
 }
 
 function GetEnemy1(wave : int, push : int, composition : int) : EnemyList{
     return amountOfWaves[wave].GetEnemy(push, composition);
 }
 
 /*NESTED CLASSES*/
 
 class WaveAmount
 {
     var name : String;
     var wavePushes : WavePushes[];
 
     function GetEnemy1(push : int, composition : int) : EnemyList{
         return wavePushes[push].GetEnemy1(composition);
     }
 }
 class WavePushes
 {
     var startTimeSinceLastPush : float;
     var enemyBuild : Composition[];
 
     function GetEnemy1(composition : int) : EnemyList{
         return enemyBuild[composition].enemy1;
     }
 }
 class Composition
 {
     //these were the variables that were capitalized
     var enemy1 : EnemyList;
     var spawn1 : SpawnPoint;
     var spawn1Count : int;
      
     var enemy2 : EnemyList;
     var spawn2 : SpawnPoint;
     var spawn2Count : int;
 }
 
 /* ENU$$anonymous$$S */     
 enum EnemyList
 {
     enemies1,
     enemies2
 }
 enum SpawnPoint
 {
     point1,
     point2
 }

This way, GetEnemy(0,0,0) called from Start will return enemy1 from the 1st wave's 1st push' first composition. There is still the problem of you trying to instantiate an enum value. You will need to deter$$anonymous$$e which gameObject to spawn based on the value retrieved from the enum.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by iHunterKiller · Mar 05, 2014 at 03:42 AM

Thank you very very much Jamora :) Your a great help. It now spawns the correct amount input of the unit selected in the location selected.

Our only problem remaining, and i think it might be a really silly or small problem, is dumping the amountOfWaves : WaveAmount array value into the totalWaves variable, so that when we set more than 1 wave we can use that var to move between them.

Ive tried so many different ways to work this in without any luck at all.

Here is the resulting code now though, with the amazing professional help from Jamora its looking really good! =D

 /* OUTERMOST CLASS*/
 var totalWaves : int;
 var currentWave : int = 0;
 var totalPush : int;
 var currentPush : int = 0;
 
 var spawning : boolean = false;
 var enemies1 : GameObject;
 var enemies2 : GameObject;
 
 var point1 : GameObject;
 var point2 : GameObject;
 
 var amountOfWaves : WaveAmount[];
 function Update()
 {
     if(!spawning)
     {
         StartCoroutine("SpawnCheck");
         spawning = true;
     }
 }
 function GetEnemy1(wave : int, push : int, composition : int) : EnemyList
     {
         return amountOfWaves[wave].GetEnemy1(push, composition);
     }
 function GetSpawn1(wave : int, push : int, composition : int) : SpawnPoint
     {
         return amountOfWaves[wave].GetSpawn1(push, composition);
     }
 function GetSpawn1Count(wave : int, push : int, composition : int) : int
     {
         return amountOfWaves[wave].GetSpawn1Count(push, composition);
     }
 
 
 /*NESTED CLASSES*/
 
 class WaveAmount
 {
     var name : String;
     var wavePushes : WavePushes[];
      
     function GetEnemy1(push : int, composition : int) : EnemyList
         {
             return wavePushes[push].GetEnemy1(composition);
         }
     function GetSpawn1(push : int, composition : int) : SpawnPoint
         {
             return wavePushes[push].GetSpawn1(composition);
         }
     function GetSpawn1Count(push : int, composition : int) : int
         {
             return wavePushes[push].GetSpawn1Count(composition);
         }
 }
 class WavePushes
 {
     var startTimeSinceLastPush : float;
     var enemyBuild : Composition[];
      
     function GetEnemy1(composition : int) : EnemyList
         {
             return enemyBuild[composition].enemy1;
         }
     function GetSpawn1(composition : int) : SpawnPoint
         {
             return enemyBuild[composition].spawn1;
         }
     function GetSpawn1Count(composition : int) : int
         {
             return enemyBuild[composition].spawn1Count;
         }
 }
 class Composition
 {
     var enemy1 : EnemyList;
     var spawn1 : SpawnPoint;
     var spawn1Count : int;
      
     var enemy2 : EnemyList;
     var spawn2 : SpawnPoint;
     var spawn2Count : int;
 }
 
 /* ENUMS */
 enum EnemyList
 {
     enemies1,
     enemies2
 }
 enum SpawnPoint
 {
     point1,
     point2
 }
 
 function SpawnCheck()
     {
         totalPush = GetSpawn1Count(0,0,0);
         if (currentPush < totalPush)
         {
             yield WaitForSeconds(/*startTimeSinceLastPush*/1);
             SpawnUnit();
             currentPush += 1;
             spawning = false;
         }
     }
     
 function SpawnUnit()
     {
         if(GetSpawn1Count(0,0,0) >= 1)
         {
             var spawnEnemy1 : GameObject;
             if(GetSpawn1(0,0,0) == SpawnPoint.point1)
             {
                 if(GetEnemy1(0,0,0) == EnemyList.enemies1)
                 {
                     spawnEnemy1 = Instantiate(enemies1);
                     spawnEnemy1.transform.position = new Vector3(point1.transform.position.x, point1.transform.position.y, point1.transform.position.z);
                 }
             }
         } 
     }   

 
Comment
Add comment · Show 1 · 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 iHunterKiller · Mar 05, 2014 at 03:51 AM 0
Share

Sorted it yay!

 function Start()
 {
     totalWaves = amountOfWaves.length;
     Debug.Log(totalWaves);
 }

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

21 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

Related Questions

Multiple Cars not working 1 Answer

JS: Access sections of an array, without activating the rest 2 Answers

Spawning off size blocks 1 Answer

Can someone help me with my script. It won't work for me. 0 Answers

How to get one script to manage many sprites 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