A script is a type of component attached to a GameObject so you need to get the Component for the script. It’s best to do this when the object is created and keeping it in a variable in the calling script so it doesn’t have to do a search every update.
What the above poster said is correct, you want to grab a pointer to the script that contains the function you wish to call.
In addition, I’m not sure why you’re using a trigger to check for the collision. You sure that’s what you want to do? Triggers only work if there’s a rigidbody involved and are more for checking if a player reaches a waypoint, steps on a platform, etc.
That might be what you’re looking for instead.
Also another tip is that a lot of times people don’t actually spawn a bullet, rather use a raycast to see if it’s going to hit something of importance. Bullets travel rather fast, not slow enough to be visible and are likely to pass through and beyond a collider in a single frame, therefore never actually registering a hit.
Yes. Two static colliders certainly can’t detect each other as by their very definition they don’t move! Static colliders are generally things like a wall, stairs or other pieces of generally non-mobile scenery. Fixed position objects so to speak.
Perhaps you can tell us more about what you’re attempting to accomplish. Shooting a bullet from a gun that hits an enemy avatar running about and causes damage? Shooting a wall or other fixed target?
With a little more detail we’d be able to guide you in the proper direction.
That’s actually pretty fun to play! You’re doing quite well for your first project. Well here’s how I would configure things.
It’s obvious that they way you’re going about things is working well, just a non-standard way of doing things with static colliders and triggers for moving objects. The difference is, say you wanted in the future to add some splash damage particles or a decal on the enemy where it collides. OnTriggerEnter wouldn’t give you sufficiently detailed information to do that. OnCollision, would.
As far as your bullets go, they’re not moving all that fast like a rifle bullet, so no need to drop down to a raycast check. As they appear to impart some force on the enemies, I assume you’re using a rigidbody with a force added to propel them. That’s perfectly fine, just use a primitive collider and not a trigger. The block eyes you would just use a regular box collider for them, again no need for a trigger. I’m guessing you’re moving them via translate. That’s fine!
Good luck with your learning experience, arcade. You’re doing well so far.
But I move the player by taking input and chaning X and Z, making it possible to add/subtract from both by holding down two keys. This makes you run faster when running diagonaly.
if (Input.GetButton("Right"))
{
pos.x += movementSpeed * Time.deltaTime; //ETC.
If you look at the above documentation, you’ll see that the gameObject you are colliding with is directly available in the collision data returned. So you can even do nifty things like directly check the object’s tag to see if you want to process the event or ignore it.
function OnCollisionEnter(collisionInfo : Collision) {
// If we collide with an enemy the play nifty sound!
if (collisionInfo.gameObject.tag == "enemy") {
audio.PlayOneShot(boomSound);
}
}