- Home /
How to access a colliders function or variable
So basically I have to cubes running at each other, PlayerCube and EnemyCube (both are prefabs that are being spawned at a constant rate). I have it set up so that once PlayerCube runs into EnemyCube or vice versa they stop moving. I would like to be able for each cube to attack each other but I am having a trouble.
Here is my current script for PlayerCube:
function OnCollisionEnter(other : Collider)
{
var contact : String = other.transform.name;
var damage = other.gameObject;
if (contact == "EnemyCube")
{
damage.Hurt();
}
}
The compiler error I am getting is
'Hurt' is not a member of 'UnityEngine.GameObject'.
Also the EnemyCube has a function Hurt() on one of its movement script that subtracts its health variable. Would it be better to directly change the variable health like so
damage.health --;
or stick to a setter method?
EDIT
So my collider is set up accordingly (I am very new to unity and programming so I might have this COMPLETELY wrong haha). On my PlayerCube and EnemyCube prefab I have a 2D rigidbody and 2D Box collider. The box collider just covers the entire cube and does not have the trigger box selected(because then it falls off my ground). My ground also has 2D rigidbody and 2D Box collider but the rigidbody is
Answer by Imankit · Nov 15, 2013 at 07:41 AM
you should do this
function OnCollisionEnter(other : Collider)
{
var contact : String = other.transform.name;
var damage = other.gameObject;
if (other.collider.name == "EnemyCube")
{
damage.GetComponent(YourClassName).Hurt();
}
}
Still not working for me! One problem that I am seeing is that these Enemy Cubes are being spawned as prefabs, so they have the (Clone) suffix at the end of the name, but even if I am checking for EnemyCube(Clone) it still doesn't work. Thank you for your previous answer btw
Answer by brain56 · Nov 15, 2013 at 08:02 AM
That's because you are sending the Hurt()
method call to a GameObject
class. What you want done is send the Hurt()
call to the component of the EnemyCube
where Hurt()
is defined. You can do this by using the `GetComponent` method:
var damage = other.gameObject.GetComponent(EnemyCubeScript);
Regarding your other question, it is thought to be more preferable to use an setter method like what you are doing to deal the damage. This way, you can encapsulate the damage computation, as well as hide what should be a private variable. This is considered good object-oriented programming practice.
EDIT:
About your cloning issue, your problem lies in this line:
if (contact == "EnemyCube")
What happens is you're looking for a collision with an object named "EnemyCube" when sometimes, the objects will be named "EnemyCube(Clone)".
A bandage solution would be to change the line to:
if (contact == "EnemyCube(Clone)")
But that's not very nice. What I recommend you to do instead is verify if the other
object is an Enemy. You can do this by searching for the script the enemy uses. If it has the script component, then it's an enemy. If it doesn't have the script component, then consider it not an enemy. You can do it this way:
function OnCollisionEnter(other : Collider)
{
var damage = other.gameObject.GetComponent(EnemyCubeScript);
if (damage != null)
{
damage.Hurt();
}
}
Still not working for me! One problem that I am seeing is that these Enemy Cubes are being spawned as prefabs, so they have the (Clone) suffix at the end of the name, but even if I am checking for EnemyCube(Clone) it still doesn't work. Thank you for your previous answer btw
Again not working, I put a Debug.LogError in the if(damage != null) statement, and I'm not getting any console message. I doubled checked to make sure the PlayerCube prefab had the above script attached and when a PlayerCube reaches an EnemyCube I can visually see a collision so I know that they are colliding. Perhaps I messed up in some collision aspect I don't know about. Do you know if Unity has a debugger where I can see if each Cube has actually collided? Again thank you for helping me out here
Put a Debug.Log()
outside the if. Put it at the beginning of your OnCollision
method. If it doesn't print, then it doesn't "collide" and maybe your collider setup is problematic.
Ahhhhh you are correct, I guess it isn't actually colliding... Why is it that the cubes stop when they hit each other? Could you recommend a correct way to set up a collider?
Edit your answer and show me how you set up your collider, maybe I can see what's wrong. You're also free to google a collider set up guide. :)
Your answer
Follow this Question
Related Questions
How to block dragging if collider hits 1 Answer
Problems with OnTriggerEnter 1 Answer
Boolean wont acitvate if the enemy enters a trigger 1 Answer
Collision, change skybox in game 1 Answer
Collide script not working... 1 Answer