Search Unity

need help with c# damage script

Discussion in 'Scripting' started by hodge47, Apr 14, 2014.

  1. hodge47

    hodge47

    Joined:
    Apr 14, 2014
    Posts:
    25
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Linq;
    4.  
    5. public class shooting : MonoBehaviour {
    6.    
    7.     public GameObject bullet;
    8.     float bulletImpulse = 75f;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.      if (Input.GetButtonDown("Fire1")){
    18.  
    19.  
    20.             Camera cam = Camera.main;
    21.             GameObject theBullet = (GameObject)Instantiate(bullet, transform.position + cam.transform.forward, transform.rotation);
    22.             theBullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
    23.  
    24.             OnCollisionEnter();
    25.         }
    26.  
    27.  
    28.  
    29.     }
    30.  
    31.     float dmg = 20;
    32.    
    33.     void OnCollisionEnter(Collider hit){
    34.         if(hit.tag == "enemy"){
    35.             hit.transform.SendMessage("Damage",dmg);
    36.         }
    37.        
    38.     }
    39. }
    40.  
    That is my shooting.cs script thats on my player and here is my enemy logic script.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enemyLogic : MonoBehaviour {
    5.  
    6.     float health = 90;
    7.    
    8.     void Damage(float dmg){
    9.         health -= dmg;
    10.     }
    11.    
    12.     void Update (){
    13.         if(health <=0){
    14.             Destroy(gameObject);
    15.         }
    16.        
    17.     }
    18.    
    19.    
    20.    
    21.    
    22. }
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    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.
     
  3. hodge47

    hodge47

    Joined:
    Apr 14, 2014
    Posts:
    25
    Okay thank you. I was going to give raycast a try anyway so ill just abandon this code and start completely fresh.