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 Rick74 · Mar 09, 2014 at 01:13 PM · javascriptarraypooling

Grabbing an object from array

Ok bear with me here, I'm only a few months into programming and I'm trying to pool my first object/array.

With this function I'm trying to

  • select a random spawn point (spawnChoice)

  • select the first available object not currently in use within the array (enemyPrefabs01)

  • check if that object within my array is deactivated

  • move that object to the position of the random spawn point selected

  • activate the object (enemyPrefab01)

       function FirstSpawn ()
                 {
                     print ("waiting for " +waitFor);
                     var holdON: float = getPlayTime + waitFor;
                     
                     
                     yield WaitForSeconds (waitFor);
                         
                     flyInMessage.GetComponent(AlertMessage).message = "Here They Come!!!";
                     flyInMessage.GetComponent(AlertMessage).PlayAnim ();
                     
                     yield WaitForSeconds (1.2);
                     
                     getSoundHelper.GetComponent(SoundHelper).PlayAlienBark03 ();
                     
                     waveLevel ++;
                     Instantiate (waveFX, waveFXplacement.transform.position, waveFXplacement.transform.rotation);
                     PlaySound (waveAudio, 0);
                     UpdateHUD ();
                     
                     var i = difficulty;
                     
                     for (; i >= 0; i--)
                     {
                         var enemyChoice = Random.Range (0, enemyPrefabs01.Length);
                         spawnChoice = Random.Range (0, alienSpawnPoints.Length);
                         var zOffSet :   float = 0.0f;
                         zOffSet = Random.Range (-.03, .03);
                         var enemyOffset : Vector3 = new Vector3( 0.0f, 0.0f, zOffSet);
                         print (enemyPrefabs01[i]);
                         if (enemyPrefabs01[i].SetActive == false)
                         {
                             print ("in here");
                             enemyPrefabs01[i].SetActive(true);
                             enemyPrefabs01[i].transform.position = alienSpawnPoints[spawnChoice].position + enemyOffset;
                         }
                         
                         //Instantiate (enemyPrefabs01[enemyChoice], alienSpawnPoints[spawnChoice].position + enemyOffset, alienSpawnPoints[spawnChoice].rotation);
                         print ( "1 enemy spawned");
                         
                         yield WaitForSeconds (.2);
                     }
                     
                     InterMission ();
                     wave ++;
                     yield;
                     
                 }
             
    
    
    

I havn't even gotten to the activation part because the function will not go inside the "if conditon", and I can't understand why.

On start I've successfully built an array, and deactivated them all. Called in function Start (). I can see they are deactivated when game is running in the hierarchy window.

 function InstantiateEnemy ()
 {
     for (var i : int = 0; i < numberOfEnemys01; i++)
     {
         enemyPrefabs01[i] = Instantiate(Resources.Load("Prefabs/Enemy Prefabs/prefab alien")) as GameObject;
         enemyPrefabs01[i].SetActive(false);
     }
 }

Seeing as how this is my first go, I could only find examples to pooling objects in C#. While the two languages are simular, I still can't seem to get this to convert successfully to Javascript.

Comment
Add comment · Show 9
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 Hoeloe · Mar 09, 2014 at 01:25 PM 0
Share

As a note, I wouldn't recommend using Javascript at all, and especially not if you're new to program$$anonymous$$g. Yes, Javascript looks easier, but that's only because it hides a lot of the complexity from you, meaning that unless you're very careful and know what you're doing, it can seriously mess up your work. I recommend giving C# a try, even if it looks more complex to you.

That said, I'm not entirely sure what you're trying to do. What do you mean when you say the function will not go inside the "if conditon"?

avatar image Rick74 · Mar 09, 2014 at 01:29 PM 0
Share

I appreciate the advice, and on my next go around I will most likely switch to C#.

But for now I'd rather just stay in Java for the duration of the project.

the if condition I'm referring too is this one

 if (enemyPrefabs01[i].SetActive == false)
                    {
                      print ("in here");
                      enemyPrefabs01[i].SetActive(true);
                      enemyPrefabs01[i].transform.position = alienSpawnPoints[spawnChoice].position + enemyOffset;
                    }

Since they all get switched off on function Start, this shouldn't be a problem. I figured it was a decent way of checking if they object is currently in use or not.

avatar image Hoeloe · Mar 09, 2014 at 01:38 PM 1
Share

Another point of note: Java is NOT the same thing as Javascript - they are fundamentally different, and it is definitely worth knowing that.

As for the code you posted... you can't really do that. SetActive is a method. As the name suggests, it is used to set the activity of an object, not to test it. SetActive is basically another "function", and you're not even calling it, but just comparing the function itself with false (which is complete nonsense). This is, in fact, one of the reasons C# would be better. If this were written in C#, the compiler would complain about this, with an error message stating exactly why you can't do it. Javascript, thanks to the monstrosity that is duck typing, just assumes that SetActive is now a boolean, which is a really, REALLY weird thing to do.

What you actually want to do is to look at the libraries and find a property called something like "active" or "isActive", which is essentially a variable that you can look at, which will tell you if the object is active or not.

Also, one more tip - if you're just doing a boolean comparison in the if statement, you don't need to write x == true or x == false, you can just write x or !x (! means "not"), which is semantically the same thing.

avatar image Rick74 · Mar 09, 2014 at 01:57 PM 0
Share

So after some fiddling, I switched;

if (enemyPrefabs[i].SetActive == false)

to

if (enemyprefabs[i].activeSelf == false)

And it seems to work fine now. thanks

avatar image Hoeloe · Mar 09, 2014 at 02:30 PM 1
Share

Pretty much. Just one thing, though. There is a difference between a function and a function call. To use SetActive as an example, this is a function:

 SetActive

And this is a function call:

 SetActive(true)

The reason I keep recommending C# is that it has a better notion of types than Javascript. Javascript has typing, but tried to hide it from the user, which can produce all sorts of weird a non-sensical code and bugs.

The first of these isn't really a variable at all, it's a language level structure, that is, it's a function.

The second, on the other hand, is an expression, and as such it can be evaluated. In this case, SetActive doesn't actually return anything, so its evaluation has no result, just side effects.

Let's say we had a function called GetActive(), though, defined like this:

 //C#
 
 public bool GetActive()
 {
     return activeSelf;
 }

 //Javascript
 function GetActive()
 {
     return activeSelf;
 }

These both have a return type, so they can be evaluated to a simple expression (another benefit of C# is that it forces you to define the return type, making it much clearer what your methods actually do), so, while GetActive is still a function type, and thus useless in expressions, GetActive() can be evaluated. This time, however, it evaluates to a boolean type, which means it can either be true or false, and thus you can use it in other expressions, including comparisons.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

Using an object pool with Javascript. 1 Answer

How to use an Array list of classes inside a class? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Making an Array of Spawnpoints... 1 Answer

How to delete new'd arrays? 2 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