Okay so im new to c# and functions and such. I only have one error "error cs1501 no overload for method 'onCollisionEnter' takes '0' arguments" and i know that this is probably simple but could someone help? Code (csharp): using UnityEngine; using System.Collections; using System.Linq; public class shooting : MonoBehaviour { public GameObject bullet; float bulletImpulse = 75f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetButtonDown("Fire1")){ Camera cam = Camera.main; GameObject theBullet = (GameObject)Instantiate(bullet, transform.position + cam.transform.forward, transform.rotation); theBullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse); OnCollisionEnter(); } } float dmg = 20; void OnCollisionEnter(Collider hit){ if(hit.tag == "enemy"){ hit.transform.SendMessage("Damage",dmg); } } } That is my shooting.cs script thats on my player and here is my enemy logic script. Code (csharp): using UnityEngine; using System.Collections; public class enemyLogic : MonoBehaviour { float health = 90; void Damage(float dmg){ health -= dmg; } void Update (){ if(health <=0){ Destroy(gameObject); } } }
Specifically, it is telling you that you cannot call the function OnCollisionEnter with zero arguments, which is what you are doing in line 24. OnCollisionEnter has to take a collider argument. However, OnCollisionEnter is not a function that you call explicitly - It will be called automatically when two objects with proper colliders/rigid bodies collide. In this case, you will want to move your OnCollisionEnter code to a script on your bullet object, which should have its own collider + rigid body so that it can run into the appropriate game objects. An important thing to note, though, is that since bullets tend to move very fast and are very small, collision detection might not be accurate enough. You may want to look into Physics.Raycast, in which you will cast a line in front of the bullet each frame to see if it will hit anything.
Okay thank you. I was going to give raycast a try anyway so ill just abandon this code and start completely fresh.