- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
IridicThresh4326 · Aug 20, 2014 at 04:24 AM ·
javascriptrandom.rangerandomization
trying to add rarity to items, don't know whats wrong with this script
#pragma strict
var Health = 100;
var Enemy : Transform;
var EnemyDrop : Transform;
var EnemyDrop2 : Transform;
var EnemyDrop3 : Transform;
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
if (Health <= 0)
{
Dead ();
}
}
if (Health <= 0)
var randomPick : int = Mathf.Abs(Random.Range(1,40));
{
if(randomPick >= 1 && randomPick <= 10){
var Item1 : Vector3 = Vector3(Random.Range(-1.0,1.0),0, Random.Range(-1.0,1.0));
Instantiate(EnemyDrop, Enemy.transform.position + Vector3(0,0,0) + Item1, Quaternion.identity);
Debug.Log("Chose pos 1");
}
else if(randomPick >= 11 && randomPick <= 20){
var Item2 : Vector3 = Vector3(Random.Range(-1.0,1.0),0, Random.Range(-1.0,1.0));
Instantiate(EnemyDrop2, Enemy.transform.position + Vector3(0,0,0) + Item2, Quaternion.identity);
Debug.Log("Chose pos 2");
}
else if(randomPick >= 21 && randomPick <= 30){
var Item3 : Vector3 = Vector3(Random.Range(-1.0,1.0),0, Random.Range(-1.0,1.0));
Instantiate(EnemyDrop3, Enemy.transform.position + Vector3(0,0,0) + Item3, Quaternion.identity);
Debug.Log("Chose pos 3");
}
}
function Dead()
{
Destroy (gameObject);
}
Comment
Everything from line 25 to line 49 is not inside a function and therefore is not getting executed.
That's why I do C#. The compiler picks up on things like that and gives an error message.
Answer by KaiUwe · Sep 02, 2014 at 10:32 PM
#pragma strict
var Health = 100;
var Enemy : Transform;
var EnemyDrop : Transform;
var EnemyDrop2 : Transform;
var EnemyDrop3 : Transform;
var RandomPick : int;
function ApplyDamage (TheDamage : int){
Health -= TheDamage;
if (Health <= 0){
Dead ();
}
}
function Dead(){
RandomPick = Random.Range(1,40);
{
if(randomPick >= 1 && randomPick <= 10){
var Item1 : Vector3 = Vector3(Random.Range(-1.0,1.0),0, Random.Range(-1.0,1.0));
Instantiate(EnemyDrop, Enemy.transform.position + Vector3(0,0,0) + Item1, Quaternion.identity);
Debug.Log("Chose pos 1");
}
else if(randomPick >= 11 && randomPick <= 20){
var Item2 : Vector3 = Vector3(Random.Range(-1.0,1.0),0, Random.Range(-1.0,1.0));
Instantiate(EnemyDrop2, Enemy.transform.position + Vector3(0,0,0) + Item2, Quaternion.identity);
Debug.Log("Chose pos 2");
}
else if(randomPick >= 21 && randomPick <= 30){
var Item3 : Vector3 = Vector3(Random.Range(-1.0,1.0),0, Random.Range(-1.0,1.0));
Instantiate(EnemyDrop3, Enemy.transform.position + Vector3(0,0,0) + Item3, Quaternion.identity);
Debug.Log("Chose pos 3");
}
Destroy (gameObject);
}
This should work for you. Everything except the variable declaration have to be in a function.
Your answer
Follow this Question
Related Questions
Is there a better way for randomization for triggering a percentage of the time 1 Answer
How to activate a script from another object? 1 Answer
Randomize values without exceeding a value? 3 Answers
How do I randomize my Music played? The opening song is always the same song and I don't know why. 1 Answer
How to make certain elements in an array rarer when using random selection? 4 Answers