- Home /
Is there anything wrong with this script? It used to work but now it doesn't.
Hello. Here is my code:
var NormalBall : Rigidbody;
var BonusBall : Rigidbody;
function Start () {
}
function SpawnBall(){
var choice = Random.Range(1, 6);
if (choice == 1){
TypeOfBall = BonusBall;
}
else {
TypeOfBall = NormalBall;
}
var clone : Rigidbody;
clone = Instantiate(TypeOfBall, transform.position, transform.rotation);
}
It is supposed to randomly pick one of two balls to spawn and do so from a game object that this script is attached to. I have both balls set in that game object, but it is not spawning them. Any ideas? Thank you!
Answer by Craze · Jan 06, 2011 at 03:05 AM
lolol, Didn't notice that the start function is empty...? he isn't even calling it ROFL, no wonder it isn't working xDD
function Start () {
SpawnBall();
}
Answer by PrimeDerektive · Jan 06, 2011 at 02:57 AM
You are not declaring TypeOfBall anywhere... you probably want to put:
private var TypeOfBall : Rigidbody;
Up top with your other declared vars.
Also, try to reserve CamelCase for functions and methods, and use camelCase from variables and properties... its good practice.
TypeOfBall is being declared, the first time it's used. "var" is optional, unless you use #pragma strict. You wouldn't want to make TypeOfBall a global variable for no reason, so the code is fine as-is, although personally I'd use var TypeOfBall = BonusBall;
to make it more clear what's happening.
Interesting, I didn't know that. I always read this line in the "head first into Unity with Javascript" page of the wiki: "The var keyword is required", without bothering to read the text under it: "In JavaScript, if you don't use the var keyword when defining a variable, that variable is scoped as a global one". Thanks!
Your answer
Follow this Question
Related Questions
Why my instantiate has error? 0 Answers
Spawn Script Issue 0 Answers
Code only affects first clone? 2 Answers
How can I assign a clone with a script to a variable? without drag & drop 1 Answer
SendMessage setName has no receiver! 1 Answer