List & Count & Add

Hi there, forigve if already answered (I’ve browsed the forum)

I’m trying to establish a communication between two scripts (first script attached to a couple of game objects, and second attached to Main Camer - probably not a best practice, but I’m learning).

So when I click on the gameObject I’d like it’s name to be added to a list which is declared in another script (the one attached to the Main Camera). What my problem is, when I type Count it seems like it’s adding 8 to the Count value of the List. Which is also weird as I’ve declared the List to accept strings.

I’d appreciate your help.

Below code snippets:

using UnityEngine;
using System.Collections;

public class AddTagToArrayList : MonoBehaviour {

    RaycastHit hit;

    void Start()
    {
        GameObject Target = GameObject.Find("Main Camera");
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
            {
            click();
            }
    }
    void setLog(string txt)
    {
        Debug.Log(txt);
    }
    void click()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 100))
        {
            Debug.Log(hit.collider.name);
            GameObject mCamera = GameObject.Find("Main Camera");
            mCamera.GetComponent<CamerScript>().add(hit.collider.name);
        }
    }
}

And here’s the code in the script attached to the Main Camera:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CamerScript : MonoBehaviour {

	public List<string> stomach;

	private void Start () {
		stomach = new List<string>();
	}
    private void Update()
    {

        if (stomach.Capacity == 1) Debug.Log("stomach Capacity = " + stomach.Capacity);
        if (stomach.Capacity == 2) Debug.Log("stomach Capacity = " + stomach.Capacity);
        if (stomach.Capacity == 3) Debug.Log("stomach Capacity = " + stomach.Capacity);
        if (stomach.Capacity == 4) Debug.Log("stomach Capacity = " + stomach.Capacity);
    }
		void seeWhatHappens(ArrayList stomach) {
        Debug.Log("W array-u są 4 elementy");
		}
        void shit()
    {
        print("shit");
        { Debug.Log("stomach more than 4  "+stomach.Count); stomach.Clear(); }
    }

        public void add(string txt)
    {
        stomach.Add(txt);
        Debug.Log("Przesłano do arraya wartosc String: "+txt);
        Debug.Log("Obecna ilosc w stomach " + stomach.;
        Debug.Log(""+stomach.ToString());
    }
}

You are talking about Count but in your script you are printing out Capacity. Which one do you mean?

Count is the amount of items you have put into the list.

Capacity is the size of the underlying array where the items are stored. Whenever you add an item and there’s no room for it, the array is doubled in size.

Count and Capacity just like all other properties of basic C# classes are well documented so you don’t need to guess what they mean

when I type Count it seems like it’s adding 8 to the Count value of the List. Which is also weird as I’ve declared the List to accept strings.

Count and Capacity are numbers that represent the size of your list. They are always integers. It doesn’t matter if your list contains horses or spaceships, the list always has Count and Capacity that describe how many of those there are or could be fitted into the list.

Your list contains strings. If you want to get one of the strings in the list, you have to reference them by their index e.g. Debug.Log(stomach[0])

Maybe it is because you are not referring what variable to add the string name, try this:
mCamera.GetComponent<CamerScript>().stomach.add(hit.collider.name);