Basic Collision Problem

Hello,

I am trying to make it so once my projectile hits the enemy they get destroyed. Its a basic 2d shooter Im making using these tutorials:

The problem is once I start the game the enemy’s just get destroyed instantly, heres my code:

void OnTriggerEnter(Collider otherObject){
		
		if(otherObject.tag == "enemy")
		{
		    		    
			otherObject.gameObject.GetComponent("Enemy_script");
			                                                                                                                                  
				
		}
	}

is it the only code in your game?
You probably call Destroy somewhere else .

Could you post the code in the enemy script?

Yeah apologies, here it is:

#pragma strict

var MinSpeed: int ;
var MaxSpeed: int ;
private var currentspeed: int;
private var x: int;
private var y: int;
private var z: int;





function Start () {

currentspeed = Random.RandomRange(MinSpeed, MaxSpeed);
x = Random.RandomRange(-224, 224);
y = 106;
z = 0.0;

transform.position= new Vector3(x,y,z); 
}



function Update () {}


public function Setposition(){

var ToMove = currentspeed * Time.deltaTime;
transform.Translate(Vector3.down * ToMove);

if (transform.position.y < -106)
{currentspeed = Random.RandomRange(MinSpeed, MaxSpeed);
x = Random.RandomRange(-224, 224);
y = 106;
z = 0.0;

transform.position= new Vector3(x,y,z); 
}
}

are you sure the enemy is destroyed and just placed somewhere far ? look at your scene inspector , click on your enemy and press “F”

Yeah, they are there before I click play but as soon as I click play they disappear, its as if the code is saying destroy yourself straight away instead of saying destroy yourself when hit by projectile.

there is no call to Destroy , what you do is setting the position of the object far away , I’m pretty sure that your object is not destroyed, but you just can’t see it .

try->

function Start () {
currentspeed = Random.RandomRange(MinSpeed, MaxSpeed);
x = Random.RandomRange(-2, 2);
y = 1;
z = 0.0;
transform.position= new Vector3(0,0,0);
}

That code makes the enemy appear in the center of screen. So they appear again but don’t fall randomly and when I change the x and y coordinates in the code they go back to not appearing

Can you create a zip and put it online?

I’ve got it uploading now, will post the link when done. About 30 minutes.

http://www.filedropper.com/2dspacegame

Thats the link above.

I found your bug , your enemy gameobject have the projectil script , so when you set the position above 100 the enemy is delete .

Remove it , and that should work

Ahh yes thankyou, it worked.

But I now somehow don’t have my enemy’s dropping down the screen. Im sure i will work it out.

I also cant move left and right but only up and down if that is a problem you can easily solve otherwise thanks for all the help.

If you can move up and down, it’s shouldn’t be difficult to move it left and right , a few things to check ( your game is in 2D with an orthographic camera , so maybe you move it’s on the wrong dimension )
-add a log in your console , to see if you enter in the condition where you move it .
-when you play your game , click on your gameobject and see in the inscpector if the position of your gameobject change.

PS : it’s better if you search by yourself what is wrong , if you are really stuck notify me and I will try to help you .

Good luck ^^