Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OptimisticCode · Nov 15, 2013 at 06:46 AM · javascriptcollideraccessing

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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();
     }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OptimisticCode · Nov 20, 2013 at 06:45 AM 0
Share

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

avatar image
1

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();
    }
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OptimisticCode · Nov 20, 2013 at 06:22 AM 0
Share

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

avatar image OptimisticCode · Nov 20, 2013 at 07:39 AM 0
Share

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

avatar image brain56 · Nov 20, 2013 at 08:43 AM 0
Share

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.

avatar image OptimisticCode · Nov 20, 2013 at 08:46 AM 0
Share

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?

avatar image brain56 · Nov 20, 2013 at 08:49 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges