Scoring and Lives?

I have this code written, it seems to work sept for the lives do not subtract when hit by enemy-

The scoring works fine-

public Texture2D btnFire;
public static int Score = 0;
public static int Lives = 3;
public GameObject AMRAM;
public GameObject FriendlyExplosion;



   void OnGUI() 
        {
		if (GUI.Button(new Rect(Screen.width/1.25f - 50,Screen.height/1.25f - 50, 50, 50),btnFire)) 
 		{
            Instantiate(AMRAM, transform.position, transform.rotation);
	  	}
        GUI.Label(new Rect(100, 10, 100, 20), "Kills: " + F35Player.Score.ToString());
        GUI.Label(new Rect(100, 30, 100, 20), "Aircraft: " + F35Player.Lives.ToString());
        }
   void OnTriggerEnter(Collider otherObject)
	    {
		if (otherObject.tag == "Enemy")
		{
		   F35Player.Lives --;
			
		   Enemy enemy = (Enemy)otherObject.gameObject.GetComponent("Enemy");
		   enemy.SetPositionAndSpeed();
			
		   Instantiate(FriendlyExplosion, transform.position, Quaternion.identity); 
		}
}
}

Any ideas

What happend if you put Debug.Log event just after the F35Player.Lives --; , does this executes ?

Tried that nothing happens- no errors or anything-

I’m stumped-

it looks like the “if (otherObject.tag == “Enemy”)” condition is not executing… you could try to change :

if (otherObject.tag == “Enemy”)

to

if (otherObject.gameObject.tag == “Enemy”)

That didn’t do it-

just to know more about the object, does the object that contains this script has a collider and marked as trigger ?, if that, make sure the collider is big enough to handle the enemy physics.

remember the object is entering to the trigger must to be characterController or Rigidbody or trigger will not work, (i guess as an enemy it has physics), check the collision detection (in case of rigidbody) setting up from discrete to continuous.

if even this does not works, uncomment the OnGUI function so the OnTriggerEnter will be alone… ím really not sure if both functions can works together in the same script …

if that does not work yet, we could talking about bug… :\

EDIT : forgot to say you… have you tried to replace the tag with name ? :

if (otherObject.gameObject.name == "gameObjectName")

Yes I have the colliders and trigger set up I will make sure the collider on the one side is good enough-

I will try that other stuff too-

Thank you for your advice-

I tried all those things and nothing worked-

so, i guess it could be a kind of bug, … if your scene is charged of objects, try the same thing in a new empty scene, that is what i do generally if weird things happens, i discard the bug instance with this…

Really its a bug- theres nothing I can do ???

I’ve tried every thing I could- as far as I know anyway-

I find it hard to believe that it just doesnt want to work- I am using the same code for the projectile collision and the score and all that works fine-

I have to be over looking something???

Make sure that your enemy is tagged as “Enemy”, and check the spelling in all places. Check that either it or the player’s collider or the enemy’s collider is marked as trigger. Make sure that one of the objects has a rigidbody attached (collisions cannot happen without a rigidbody).

If all of those things are true, make sure that the collision is hitting (Put a Debug.Log(“Collision”) inside the trigger, but outside any if statements. If it is colliding, then it is a problem with your enemy and you need to make sure the tag is proper. The above suggestion of changing it to otherObject.gameObject.tag==“” may help.

If the collision ISN’T happening, then make sure that the colliders are actually making contact. Make them very large just to test it, and it might help to turn the Time.timeScale down to 0.1 or something so that you can watch it in slow motion.

Those would be my debugging steps in your situation. Hope that helps

I appreciate your help- but I did all those things- some more than once-

I debug the collision and see-

Yeah debugger said nothing-

I am confused as to why you are using static variables, but in the case of what you are working with, it should decrement.

Try running some debugging on the OnTriggerEnter and get its state at different points.

	void OnTriggerEnter(Collider otherObject){
		Debug.Log("Object Entered: " + otherObject + " Tag: " + otherObject.tag);
		if (otherObject.tag == "Enemy"){
			Debug.Log("Enemy Entered Trigger: " + otherObject);
			F35Player.Lives --;
			Debug.Log("Lives Decremented: " + F35Player.Lives);
				
			Enemy enemy = (Enemy)otherObject.gameObject.GetComponent("Enemy");
			Debug.Log("Enemy Unit Script: " + enemy);
			enemy.SetPositionAndSpeed();
			Debug.Log("Enemy Unit SetPositionAndSpeed Used!");
				
			GameObject explosion = Instantiate(FriendlyExplosion, transform.position, Quaternion.identity);
			Debug.Log("Explosion Created: " + explosion);
		}
	}

I tried it Big- got nothing in the console???

WTH

Your collider is obviously not triggering. Could you post screenshots of the inspector for the Enemy gameObject, and the Player gameObject?

Here are the inspectors-


isTrigger is NOT checked. :wink:

On the F35Player it is BIG- I tried it on both of them-