If/Else statements effecting only specific tags

Hi,

I want to create various scenarios that effect only specific gameObjects with specific tags. These scenarios all come in the form of if/else statements. For these examples, let’s just include the tag “enemy” as our example.

The goal is to have A) time effect the enemy, but not me. B) have the enemy’s position effected, but mine is unaffected. Obviously, the only hard part is applying the gameObject’s tag correctly. Thus, I will just provide a brief sample of scenario A. Here is the code:

if (Input.GetKey("z"))
	{
	Time.timeScale = 0.5;
	}

This code is clearly not complete. I am wondering how I add the
if (other.gameObject.tag=="enemy")
portion of the code so that I can isolate the effects to the enemy. I’m sure I can write the other part of the code, I just need help with this part. Sorry if this question seems really obvious and stupid, but it is causing some confusion for me.

Thanking everyone in advance for taking a look at this, and/or for trying to help.

Two possibilities:

  1. Tag your game objects as you need in the editor. Then in your script use FindGameObjectsWithTag for each of your tags and store the result in arrays (as per example on the doc page). Just avoid doing these finds in Update(), because they’re very expensive. Do them in Start() for example. Now you can loop over all your enemies or player or whatever and modify them.

  2. Make individual scripts for your enemies and players and attach them to them. Then you can put stuff like this in your Enemy script, and it will only affect enemies:

    function Update(){
    if(Input.GetKeyDown(“S”))
    transform.localScale*=0.5; // use shrink ray on enemy
    }

These are pretty basic principles, so I’d suggest to do some tutorials about player handling etc.