- Home /
Spawning Enemy
I am trying to spawn an enemy within the vicinity of the player when a trigger is entered. Right now I have the enemy as a prefab. I am using this code to spawn him in
var entity: Transform;
function OnTriggerEnter(collider : Collider) {
if(gameObject.tag=="Spawn") {
Debug.Log("Enemy should spawn"); Instantiate(entity, transform.postion + ( transform.right * 10), Quaternion.identity );
} }
I attach the prefab in the editor to entity
I keep getting an error stating NullReferenceException: Object reference not set to an instance of an object.
Any idea of what I am doing wrong?
Answer by BeHappy · Jul 18, 2012 at 10:26 AM
Try changing,
if(gameObject.tag =="Spawn")
to
if(collider.gameObject.tag =="Spawn")
If it doesn't work, please provide with detailed exception message.
This is still not working. I get a NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String, operatorName, System.Object lhs, System.Object rhs)
this is the script now
var entity: Transform;
function OnTriggerEnter(collider : Collider) {
if(collider.gameObject.tag=="Spawn") {
Debug.Log("Enemy should spawn"); Instantiate(entity, transform.postion + (transform.right * 10), Quaternion.identity);
} }
If I replace the transform.position +(transform.right * 10) with Vector3() it spawns an enemy but I want it to be somewhere near the player. I am pretty sure it is something with that part of the script.
Just to be clear I am using javascript
in the example, it says "transform.postion", that's the null reference
Answer by Seth-Bergman · Jul 19, 2012 at 04:47 AM
function OnTriggerEnter(collider : Collider) {
if(collider.gameObject.tag=="Spawn") {
var nearbyPosition : Vector3 = transform.position;
var signSpinner = Random.Range(1,10);
var multiplier = 1;
if(signSpinner%2 == 1)
multiplier = -1;
var posX = Random.Range(5.0,10.0) * multiplier; //or whatever range
signSpinner = Random.Range(1,10);
multiplier = 1;
if(signSpinner%2 == 1)
multiplier = -1;
var posY = Random.Range(5.0,10.0) * multiplier;
nearbyPosition.x = transform.position.x + posX;
nearbyPosition.y = transform.position.y + posY;
Debug.Log("Enemy should spawn");
Instantiate(entity, nearbyPosition, Quaternion.identity);
} }
this would randomly pick a nearby spawn location..
EDIT: updated code to include all directions.. as for your code above, you spelled "position" wrong.. transform.postion
I ended up getting it work with this
var position: Vector3 = Vector3(Random.Range(-75, 75), 0, Random.Range(-75, 75));
Instantiate(entity, transform.position +(position), Quaternion.identity);
thanks.
no prob.. fyi, the only reason for my much lengthier code was to avoid spawning on top of the player.. not likely if the range is this large, but still could occasionally happen.. of course a simple distance check would take care of that, nice streamline of my bulky code!
Good call i did overlook spawning on top of the player. I'll look into that. Do you have any idea of how I could check the terrain height to insure the enemy always spawns above it.
I am assu$$anonymous$$g I would use Terrain.activeTerrain.SampleHeight();
Answer by CzechBuh · Jul 31, 2016 at 03:04 PM
Different version for noEnenmy character: https://www.youtube.com/watch?v=L49aHXd9e5o
This script is very simple and fast for your megacity. You can place unlimited character with animations.
Your answer