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());
}
}