- Home /
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.
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"
?
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.
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.
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
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.
Your answer
Follow this Question
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