Problem with my script?

Hello. I am get my script into the scene, but when I try to add it to an object, it says “Can’t add script behaviour Spawn2.0. The scripts file name does not match the name of the class defined in the script!”. What is wrong? Thanks.

var prefab : GameObject;
var position = Vector3(Random.Range(481.5735, 559.3441), -791.811, Random.Range(380.1254, 420.0663));

if (Projectile.Defeat < 10)
	{ 	yield WaitForSeconds(5.0); 
    	Instantiate(prefab, position, Quaternion.identity);
	}

if (Projectile.Defeat < 20  Projectile.Defeat > 10)
	{ 	yield WaitForSeconds(5.0); 
    	Instantiate(prefab, position, Quaternion.identity);
	}
	
if (Projectile.Defeat < 80  Projectile.Defeat > 20)
	{ 	yield WaitForSeconds(5.0); 
    	Instantiate(prefab, position, Quaternion.identity);
	}

I am also trying to make a script where there are multiple possible object variants to spawn (in this case enemy types) and the it randomly spawns a set number of them at repeating intervals, but when I try my script I get the error " No appropriate version of ‘UnityEngine.Random.Range’ for the argument list ‘(UnityEngine.GameObject, UnityEngine.GameObject, UnityEngine.GameObject)’ was found."

I also tried Random.value and I get the error “It is not possible to invoke an expression of type ‘float’.”

How can I fix my script?

var A : GameObject;
var B : GameObject;
var C : GameObject;

if (Projectile.Defeat < 200  Projectile.Defeat > 80)
    {   yield WaitForSeconds(5.0); 
        Instantiate(Random.Range(A,B,C), position, Quaternion.identity);
    }

For the first problem, you’re likely trying to save the script as something other than .js

For the second problem, Unity - Scripting API: Random

Random.Range is designed to generate a number between two specific values. Not a letter. You could write a statement to determine which of your game-objects to use based on the number generated.
-Rob

Thanks. But I am saving the script as .js, so any other ideas?

Yes, you did not wrap your code in functions.

var prefab : GameObject;
var position = Vector3(Random.Range(481.5735, 559.3441), -791.811, Random.Range(380.1254, 420.0663));

function Update() {
if (Projectile.Defeat < 10)
	{ 	yield WaitForSeconds(5.0); 
    	Instantiate(prefab, position, Quaternion.identity);
	}

if (Projectile.Defeat < 20  Projectile.Defeat > 10)
	{ 	yield WaitForSeconds(5.0); 
    	Instantiate(prefab, position, Quaternion.identity);
	}
	
if (Projectile.Defeat < 80  Projectile.Defeat > 20)
	{ 	yield WaitForSeconds(5.0); 
    	Instantiate(prefab, position, Quaternion.identity);
	}
}

for a quick example. Did not check or correct any code, just wrapped it in the function.

Nevermind (I had a CS script called “Spawn” and this script was “Spawn2.0”, I guess that messed it up.

Unless your c# script was the one getting the error, that wouldn’t of had anything to do with it.
-Rob

Hmm, but I was adding the spawn2.0 script, weird. Anyways, its working know.

How would I define a variable as a number in an array?

Not really a need for an array in that instance. You could, for example, do something like this:

var A : GameObject;
var B : GameObject;
var C : GameObject;

function MyFunction() {
  var Create : GameObject;
  var rndmInt : Int;
  
  rndmInt = Random.Range(1,3);
  switch(rndmInt) {
    case 1:
      Create = A;
      break;
    case 2:
      Create = B;
      break;
    case 3:
      Create = C;
      break;
    default:
      //should never make it here
      break;
  }

  if (Projectile.Defeat < 200  Projectile.Defeat > 80) {   yield WaitForSeconds(5.0); 
        Instantiate(Create, transform.position, Quaternion.identity);
    }
}

Just tossed in a switch as an example. Whenever you call MyFunction() it would randomly generate one of the objects from A, B, or C.

Thanks. I Is there any way to have the script instantiate the multiple objects at different positions within the position range?
I tried copying the instantiate part of the script, and although the objects that are spawned are different, they all spawn at the same place.

Use Random.Range to generate random numbers to use for your spawn location.

But I am also using random range to find the initial spawn point? Also, is there a better way to specify the number of objects I want instantiated other than just copying the “Instantiate(objs[(Random.Range(0, 3))], position, Quaternion.identity);” part of the function?

You can use Random.Range over and over. All it does is generate a number between two numbers you give it. So, you want to take the position and add or subtract to it to give yourself a range to generate between.

And there are many ways to decide how many of an object to spawn. There are even ways to choose this randomly.

int numberToSpawn = Random.Range(1,100);
for (int i = 0; i < numberToSpawn; i++)
{
	Vector3 spawnPosition = new Vector3(origin.X + Random.Range(-10, 10), origin.Y + Random.Range(-10, 10), origin.Z + Random.Range(-10, 10));
	Instantiate(obj, spawnPosition, Quaternion.Identity);
}