Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Trying to add multiple values into a list

Discussion in 'Scripting' started by intimidate2161, Sep 17, 2018.

  1. intimidate2161

    intimidate2161

    Joined:
    Jun 2, 2018
    Posts:
    4
    I am working on a FPS game and cannot figure out how to add multiple values to a list. I have been searching for some examples how to go about it and found one I thought would work. I am new to C# and designing videos games as I previously working with PHP and databases, I am working on this game in my spare time after work. Majority of my code was procedural, I didn't need to work with OOP much.

    What I want to do is after I find an enemy I want to store it in a list along with the current health level. There is a chance the player can be attacked by multiple enemies at the same time. Once any of the enemies dies they can be removed from the list. I came up with the idea of storing the values in a list after reading about lists and arrays. I get an error when I try to add values into my list.

    This is what I came up with while watching a video. This script is attached to an empty object in my scene.
    Code (CSharp):
    1. public class EnemyList : MonoBehaviour
    2. {
    3.    // This script will create a list that will contain a pair of items
    4.     // First item will be an object (enemy)
    5.     // The second item will be an integer (health)
    6.     public GameObject enemy;
    7.     public int health;
    8.  
    9.     public void Enemies(GameObject newEnemy, int newHealth)
    10.     {
    11.         enemy = newEnemy;
    12.         health = newHealth;
    13.     }
    14. }
    This is the code that I am using to try and add the values. The bolded line is the line that adds the values to the list, but I get an error "Assets/Scripts/CurrentEnemies.cs(59,37): error CS1729: The type `EnemyList' does not contain a constructor that takes `2' arguments". I have been trying to understand how this is suppose to work, but can't wrap my head around it. When I hover over List<EnemyList> enemies = new List<EnemyList>(); I get "List<EenemiList>.List() ( + 2 overloads )".

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CurrentEnemies : MonoBehaviour
    6. {
    7.     // Array of enemy tags used in game
    8.     public string[] names = new string[] {"Huge", "Large", "Medium", "Small", "Tiny"};
    9.     // Array of health points based on tag name
    10.     public int[] health = new int[] {30, 25, 20, 15, 10};
    11.  
    12.     public GameObject currentHitObject;
    13.  
    14.     public float sphereRadius;
    15.     public float maxDistance;
    16.     public LayerMask layerMask;
    17.  
    18.     private Vector3 origin;
    19.     private Vector3 direction;
    20.  
    21.     private float currentHitDistance;
    22.  
    23.     List<EnemyList> enemies = new List<EnemyList>();
    24.        
    25.     void Update()
    26.     {
    27.         origin = transform.position;
    28.         direction = transform.forward;
    29.         RaycastHit hit;
    30.         if (Physics.SphereCast(origin, sphereRadius, direction, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
    31.         {
    32.             currentHitObject = hit.transform.gameObject;
    33.             currentHitDistance = hit.distance;
    34.  
    35.             // I need to veryify that the tag matches the correct enemy, HugeSpider1 = Huge
    36.  
    37.             // This checks to see if the enemy is already in the list
    38.             bool spiderEnemy = false;
    39.             int enemyId = -1; // Set to -number because 0 is a valid array index value
    40.             foreach (EnemyList currentEnemy in enemies)
    41.             {
    42.                 if (currentHitObject == currentEnemy)
    43.                 {
    44.                     spiderEnemy = true;
    45.                     break;
    46.                 }
    47.             }
    48.            
    49.             // This checks to see if the tag is in the names array
    50.             foreach (string checkTag in names)
    51.             {
    52.                 if (checkTag.Equals(hit.transform.tag))
    53.                 {
    54.                     // If enemy is not in the list we can add it
    55.                     if (spiderEnemy == false)
    56.                     {
    57.                         // Here we can add the enemy object and the enemy health
    58.                         [B]enemies.Add(new EnemyList(currentHitObject, checkTag));[/B]
    59.                     }
    60.                     break;
    61.                 }
    62.             }
    63.         }
    64.         else
    65.         {
    66.             currentHitDistance = maxDistance;
    67.             currentHitObject = null;
    68.         }
    69.     }
    70. }
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Your constructor of EnemyList is wrong, should be:
    Code (csharp):
    1.  
    2. public EnemyList(GameObject newEnemy, int newHealth)
    3. {
    4.     enemy = newEnemy;
    5.     health = newHealth;
    6. }
    7.  
    Really has nothing to do with the list.
     
  3. intimidate2161

    intimidate2161

    Joined:
    Jun 2, 2018
    Posts:
    4
    Thanks for the fast reply. I studied C# list and figured out how to use it.
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    EnemyList is a MonoBehaviour, and as such you shouldn't be using Constructors, or creating instances with the new keyword. This will cause warnings in the console.

    It looks like there is no reason for EnemyList to be a MonoBehaviour, so you can probably just remove that.

    Code (CSharp):
    1. public class EnemyList
    2. {
    3.    // This script will create a list that will contain a pair of items
    4.     // First item will be an object (enemy)
    5.     // The second item will be an integer (health)
    6.     public GameObject enemy;
    7.     public int health;
    8. public EnemyList(GameObject newEnemy, int newHealth)
    9. {
    10.     enemy = newEnemy;
    11.     health = newHealth;
    12. }
    13. }