- Home /
Why do these variables work without declaring type? (JS)
I have a script here that I'm using for a little game I'm making where the player squishes mosquitos. This script is designed to destroy a prefab and replace it with a pair of new prefabs, and I made it a couple days ago by pulling some bits of code from various tutorials and guides. It works fine, but now that I'm looking back over it, it doesn't make sense and now I'm confused again.
The code looks like this:
var bloodSplatters : GameObject[]; //Creates an array that stores all bloodSplatter objects
var deadSkeeter : GameObject;
function OnMouseDown () {
//Defines a variable that creates a random rotation on the Z axis
var randomZRotation = Quaternion.Euler(0,0,Random.Range(0,360));
//Defines a random variable as a picker for the bloodSplatters
var bloodSplatterIndex = Random.Range(0,9);
//Instantiates a blood splatter and a dead mosquito and destroys the "live" mosquito
var numRandom : float = (Random.Range(0.8,1.2));
var objectMade : GameObject = Instantiate (bloodSplatters[bloodSplatterIndex], transform.position, randomZRotation);
objectMade.transform.localScale = Vector3.one * numRandom;
Instantiate (deadSkeeter, transform.position, randomZRotation);
Destroy (gameObject);
}
This is attached to the prefab that I'm destroying. I have 9 "bloodSplatter" objects stored in the array declared at the top. What I realized (I'm really new, should have seen this immediately I suppose, but I didn't) when I looked back at the code is that "randomZRotation" and "bloodSplatterIndex" are just initialized but the type is never declared. Then I figured maybe they just default to a type based on what they're initialized as, but Random.Range should return a float and I intended for "bloodSplatterIndex" to be of type int, and I never did any sort of rounding to clean up generated float values.
Now I want to clean up the code, but I don't want to break my working script, and I'd like better understanding of what I'm doing before I move on. Help?
This is normal in JS; add pragma strict and see if it goes BOO$$anonymous$$ (and depending on your philosophy, this is why you should have pragma script)
Random.Range
with int inputs returns an int. So your guess about guessing the type on 1st use is consistent (and, I think, correct.)
FYI, standard randoms only use ints. Turns out Unity's Random.Range(float, float)
is the oddball.
$$anonymous$$any thanks all! Interestingly, the code worked fine with #pragma strict as well. I've made the following changes:
#pragma strict
var bloodSplatters : GameObject[]; //Creates an array that stores all bloodSplatter objects
var deadSkeeter : GameObject;
function On$$anonymous$$ouseDown () {
//Defines a variable that creates a random rotation on the Z axis
var randomZRotation : Quaternion = Quaternion.Euler(0,0,Random.Range(0,360));
//Defines a random variable as a picker for the bloodSplatters
var bloodSplatterIndex : int = Random.Range(0,9);
//Instantiates a blood splatter and a dead mosquito and destroys the "live" mosquito
var numRandom : float = (Random.Range(0.8,1.2));
var object$$anonymous$$ade : GameObject = Instantiate (bloodSplatters[bloodSplatterIndex], transform.position, randomZRotation);
object$$anonymous$$ade.transform.localScale = Vector3.one * numRandom;
Instantiate (deadSkeeter, transform.position, randomZRotation);
Destroy (gameObject);
}
It works, but can anyone suggest changes? I just kind of guessed as far as declaring "randomZRotation" as type "Quaternion" since that seems to make sense, and I'm unsure if I should put any rounding in since I declared "bloodSplatterIndex" as int right off the bat.
Also, I have 9 game objects in the array. Should Random.Range be "0,9" or something else?
What if I wanted to, say, use Random.Range(0,arrayCount) where "arrayCount" was an integer that automatically updated to the number of objects in the array? Would that be possible? I guess that may be more of a question for the forums...
Answer by Eric5h5 · Mar 11, 2014 at 11:31 PM
The type is declared, it's just done implicitly by looking at the value. These statements are 100% identical:
var foo = 5;
var foo : int = 5;
Since "5" is an int, foo becomes an int. If you used "5.0" or "5.0f", then foo would become a float. In both cases the type is statically declared and can't be changed later...this has nothing to do with dynamic typing, so #pragma strict has no effect, and C# has the same feature:
var foo = 5;
int foo = 5;
Answer by screenname_taken · Mar 11, 2014 at 10:22 PM
It just guesses what it should be by what you are doing on it later on. Not a good thing to do. Just round it off and declare it as you wanted.
Your answer
Follow this Question
Related Questions
How do i declare a negative float while initializing a vector? 1 Answer
C# - Appropriate place for constants, variables, declarations, and initializations. 0 Answers
IntVector2 Struct useful? 1 Answer
transform.localEulerAngles.y = 180 but not less-or-equal 180.0f 1 Answer
Car keeps pulling itself out of the map. Beginner needs help 0 Answers