- Home /
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.
Time.timeScale
is a global parameter, you can't change it per object. Your only chance is to scale all time based transformations, animations (via animation.speed), iTweens, and so on on your own for your enemy objects.
ok, I accept what you say about scenario A, but what about scenario B? what if I want to press a button and move the enemy while the "Player" or "Hero" stand still?
You mean while Player and Hero are "frozen"? Then you would also need to implement your individual time scale factor as for the enemy, and apply them to your player. However, what you need to do depends on how you move/transform your player. If it's by something like transform.position+=move*Time.deltaTime, you can just add a "*myTimeScale" after that. The same goes for rigidbody.$$anonymous$$ovePosition(). If it's animations, you'd do something like animation.speed=myTimeScale. However, not sure if it's possible if you use rigidbody.AddForce() or similar.
the player need not be frozen. let's say that he is walking or standing or running, then he hits a button or taps the screen or whatever. How do I make it so that the effect only effects tags labeled "enemy" There are many effects on the enemy that I want applied in this way, but I don't know how to just isolate the enemy. in this example, how would I make it so that the Vector3(0, 0, 0) was affected. let's say it was Vector3(0,25, -250) or something like that.
Answer by Wolfram · Jan 13, 2013 at 03:08 AM
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.
I appreciate this code, but I am wondering how to reference a tag such as "enemy" so that certain effects, such as your "shrinkRay" would affect only the "enemy" Let's say I wanted to hit a button and push the enemy back, or as in your example, shrink the enemy, and/or shrink his mass, gravity, or any number of variables that I might want to play with
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613090840im_/https://answers.unity.com/themes/thub/images/avi.jpg)