Quantcast
Channel: Latest Questions by OptimisticCode
Viewing all articles
Browse latest Browse all 6

How to access a colliders function or variable

$
0
0
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

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>