GetComponent problem.

So im trying to make this work, basically when an enemy is hit, it will stop, but if theres 2 enemies, both will stop, but i want the one i hit to stop only.

	  if (hit.collider.transform.CompareTag("Monster")) 
      {
		var hurt :AISplicer = GetComponent (AISplicer);
		hurt.isHurt = true;
		yield WaitForSeconds(0.2);
		hurt.isHurt = false;
}

Thats for the gun script, and for the enemy script

function Update (){
//if the enemy is hurt, then make then pause for a sec
if(isHurt){
SendMessage ("Stop", SendMessageOptions.DontRequireReceiver);
}else{
speed = 6;
}
}

It runs with no errors, then when i hit the enemy, i get this error:

Anyone know why this wont work?

You’re not grabbing the componenet of the monster you’re hitting, your grabbing the componenet in its pre-made form, which then applies it to all enemies.

Something else to consider, is that performance speaking, it would likely be quicker for you to put the collision check on the monster for when the bullet hits the enemy. This would avoid the search for the component.

Yeah it wasnt the best way of doing this because i dont have a lot of scripting knoweldge, how would you script it? And does it matter whether hit is a boolean or not? because im just defining it as an instinitate variable (from what I think it is) then putting if hit = true/false. Whats the best way to script this?

To be honest, I think the best way to handle this would be using a collider. When the collider detects the collision, it will fire a collision event.

Don’t double your workload by reinventing the wheel. Use built-in behavior where you can.

I am using a collider, but what im trying to say is if theres 2 things sharing the same script, it will affect both of those things, both will stop, both will be in sync, i only want the one that i hit, i was told the best way to do this was using getComponent, obviously somethings wrong…

Yeah… You should be interacting with a GameObject, not a component, I’m pretty sure. I’m not sure how components handle hierarchy, but this should the the current GameObject.

So call this.ishurt, and modify this.canmove, or whatever.

if (hit.collider.transform.CompareTag("Monster")) 
      {

Theres the collider, i dont understand why u want me to make another collider, if it collides then the boolean variable should be set to true, and if set to true, it should access the Stop(); function. I just dont know how to fix up the scripts i posted above to make each enemy independent so it doesnt effect all of them…

var hurt :AISplicer = GetComponent (AISplicer);

This is why it is accessing ALL of them.

But i was told it shouldnt, thus why i did not use static varibles, bcause im 100% sure that statics will effect all gameobjects aswell… What do i do to change that?

…I’m not going to hold you hand on this one. I’ve been telling you the same thing since post one. This is the MOST specific example I’m going to give you until you start showing specific code that proves that you are actually attempting to fix this.

You need to be using the OnColliderEnter() message.

OnCollisionEnter will return a Collision object. This Collision object has a property called gameObject. Access this to determine if it is an enemy. If it is, use THAT gameObject to do the action.

In the future, if your code doesn’t work, it doesn’t work, and it shouldn’t work. Don’t look at your code and say: “This should work”. If it doesn’t, it doesn’t, it’s as simple as that. I’m not trying to scold you too harshly, but I don’t think you have understood what I have been telling you since my first reply, that you are simply trying to do something quite silly, and that the solution is much simpler than you could possibly have imagined.

No, you were told that IT SHOULD by two separate people in this thread, and you completely ignored them.

Well… i cant use an oncollisionenter, simple as that because my weapon uses raycasthit and i cant be springing out more functions to do something that is already done (if (hit.collider.transform.CompareTag(“Monster”)) )

I cant move the Collision check into the enemy because thats going to be a real issue in the future when theres more enemies, weapons, etc.

Is there any last way to do this or am i just out of luck…

And no, i ignored nobody, i was told by another person that get component will solve my issue, clearly it didnt so now i need to find a new way.

You are absolutely irrevocably wrong on nearly every count. Reread this entire thread with a set of fresh eyes, and read the material you have been given. Raycasting against rigidbodies are a form of using colloiders.

A Raycast struct will return the rigidbody that it hits, and you can access the gameObject whose rigidbody by doing the EXACT same thing as I suggested last post:

hit.rigidbody.gameObject
hit.collider.gameObject

A rigidbody functions as a collider, and a collider of some sort MUST be used in order to interact with a raycast. So yes, you DO have to use colliders.

[edit:] I’m really trying to avoid being rude here, but you are pushing all of my buttons right now, by asking for help, and not putting any sort of effort into actually listening to what people are telling you. I’ve been saying the same thing to you for hours, and yet, you still haven’t even asked for clarification about something that you clearly don’t seem to be understanding.

then suppose i have the collision check function on the enemy’s script, whats my next move?

YOU ARE NOT LISTENING TO WHAT IS BEING SAID.

I never told you to have the collision check being done in the enemy’s script, because that just doesn’t make ANY sense.

Your gun code needs to be calling the ray trace, which it already is.

I gave you the solution over and over again in my prior posts.

I’m going to make this even EASIER for you to sift through:

These are the properties you need to be using:

hit.rigidbody.gameObject
hit.collider.gameObject

I apologize for confusing you with the subject of onCollisionEnter, I was a little unclear with that, but I could have sworn we’d already made it clear that you needed to be accessing gameObjects and not components during the first post. The first two posts in this thread pointed this out to you.

FFS. I don’t want to give you code, because you clearly have not read half of what people were telling you, or I wouldn’t be on page 2 right now…

I’d have preferred you actually learned something from this, but your complete refusal to do so is only going to hurt you in the long run as a programmer.

     if (hit.collider.gameObject.CompareTag("Monster"))
      {
      var hurt:GameObject = hit.collider.gameObject;
      hurt.isHurt = true;
      yield WaitForSeconds(0.2);
      hurt.isHurt = false;
}

Do you see what I’ve been telling you now?

Yeah im understanding this now, the variable effects what is being hit, instead of everything… only problem is im getting an error :frowning:

How does the script detect isHurt without referencing it from the enemy script

Oooh… Now you are getting into a unity-specific sort of thing that I’m going to have to do some research on. (I’ve had less than two days’ experience with unity… But more than a decade of programming experience.) This is precisely the issue I don’t care for about unity, the way that it handles GameObject typing, and data manipulation…

I’ll get back to you shortly on this one.

Alright: Check this out:

Now we’re going to get into some of the design concepts that you prematurely started nit-picking about earlier, location of code between enemies and weaponry.

I personally think that the weapon AND the enemy need to interact whenever a “hit” is made. As such, I think that both the weapon and the enemy need to have bits of the damage code in them.

Check this out:

weapon:

hit.collider.BroadcastMessage("weaponHit",this);

enemy behavior:

void weaponHit(MonoBehavior weapon)
{
  weapon.BroadcastMessage("doDamage",this);
}

And back to weapon behavior:

void doDamage(MonoBehavior target)
{
  target.isHurt = true;
  yield 2;
  target.isHurt = false;
}

This seems REALLY wonky, so just… you know… Let me know if it doesn’t work for you, and if anybody knows a better way, be sure to let me know, because this is the simplest way I could think of to handle this sort of behavior.

Gave it a shot and i seemed to get this error

I put in everything else fine, but this doesnt fit, its under the lineIonPistol.BroadCastMessage("doDamage",this);
Im pretty sure you ment “weapon” as the name of my weapon script so i just changed it and the error popped up.