Search Unity

There is no argument given that corresponds to the required formal parameter, trying to understand

Discussion in 'Scripting' started by danmct1995, Jul 6, 2020.

  1. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    So I am trying to build a live system for a game, and I think I'm pretty close to having what I need. I can successfully redirect the player to a game-over screen if the life system is taken out and they lose once. The problem is calling my method for checking if the player still has any lives left or not because Unity is telling me that a problem lies with my variable. I have tried some searching on my own to fix this problem, but it seems that people mainly run into this error code by using two parameters and I only have one. Why is this error code throwing? In my code the error is thrown at CheckIfNoBallsAreLeft(); within the update area.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Lives : MonoBehaviour
    7. {
    8.     public GameObject[] hearts;
    9.     public int life;
    10.     private bool dead;
    11.  
    12.     SceneManager sceneManager;
    13.     private void Start()
    14.     {
    15.         life = hearts.Length;
    16.     }
    17.     void Update()
    18.     {
    19.         CheckIfNoBallsAreLeft();
    20.         if(dead == true)
    21.         {
    22.             // if lives reach zero death is confirmed and start scene is reloaded
    23.             FindObjectOfType<SceneLoader>().LoadStartScene();
    24.         }
    25.     }
    26.  
    27.  
    28.     public void CheckIfNoBallsAreLeft(int lifeNum)
    29.     {
    30.         int NumberOfBallsLeft = FindObjectsOfType<Ball>().Length;
    31.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    32.  
    33.         if (NumberOfBallsLeft == 0 && currentSceneIndex != 5)
    34.         {
    35.             life -= lifeNum;
    36.             Destroy(hearts[life].gameObject);
    37.             if (life < 1)
    38.             {
    39.                 dead = true;
    40.             }
    41.         }
    42.     }
    43.  
    44. }
    45.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    On line 28 you see CheckIfNoBallsAreLeft requires an integer parameter. You don't have any version of this method which takes 0 parameters. On line 19 you are calling CheckIfNoBallsAreLeft with 0 parameters, but again a 0 parameter version of that method doesn't actually exist. So you get an error.

    Either rewrite CheckIfNoBallsAreLeft so it doesn't need any parameters, or provide it an integer.
     
  3. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    Okay, so I think taking my life integer as the parameter would complete what I need. thx
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you do that, then on line 35 you will only end up with life = 0. You might as well just change that line to life = 0 and then you don't even need the lifenum parameter, as it would be pretty much pointless (any integer minus itself is always 0).