How to pass an argument to a function in another script? (c#)

Hey to everybody,

i searched but i can’t find the answer to this question, maybe because my english isn’t verry good acctualy (germanman).
Would be nice if somebody could help me out.

here what i want to do:

//i want to pass an arument to function AddItem:

using UnityEngine;
using System.Collections;

public class PickUpItem : MonoBehaviour {

public static bool pickUpItem = false;
private int chooseRandomItem = 0;
public int countItemsToPickUp;
private int countItemsPickedUp;
// Use this for initialization
void Start () {
countItemsPickedUp = 0;
}

// Update is called once per frame
void Update () {

}
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == “Player”)
{
if (pickUpItem)
{
chooseRandomItem = Random.Range( 1, 3);
countItemsPickedUp ++;
switch (chooseRandomItem)
{
case 1:
Inventory.AddItem(1);
break;
case 2:
Inventory.AddItem(2);
break;

case 2:
Inventory.AddItem(3);
break;
}
if (countItemsPickedUp >= countItemsToPickUp)
{
Destroy(gameObject);
}
}
}
}
}

//in another script:
(…)
void AddItem(int id)
{
foundInventoryItemSlot = false;
for (int i = 0; i < inventory.Count; i++)
{
if (inventory*.itemID == id)*
{
for (int j = 0; j < database.items.Count; j++)
{
if (database.items[j].itemID == id)
{
if (stack*.stackCount < database.items[j].itemInventoryStack)*
{
stack*.stackCount += 1;*
}
foundInventoryItemSlot = true;
}
}
break;
}
}
if (!foundInventoryItemSlot)
{
for (int i = 0; i < inventory.Count; i++ )
{
if (inventory*.itemID == -1)*
{
for (int j = 0; j < database.items.Count; j++)
{
if (database.items[j].itemID == id)
{
inventory = database.items[j];
stack*.stackCount = 1;*
}
}
break;

}
}
}
}
(…)
Thanks in advance.

please use code tags when you paste in code: Using code tags properly - Unity Engine - Unity Discussions

  1. Its called german. :wink:

  2. You are calling AddItem by class name. Therefore AddItem would need to be declared as static.

  3. You need to declare AddItem as public, otherwise you cant access it.
    → public static void AddItem( int id ) {}

  4. If you dont want to make AddItem static, you’d need to create an instance of the class which holds the method.

  5. Use code tags!

you might want to look at the latest unity youtube vid (i.e. by the unity peeps). It covers the basics of scripts “talking” to each other.

Its aswell about the basic concepts of programming he’s missing.

Use other.GetComponent().AddItem(1) instead to get the instance attached to the other object. You are trying to call a static method.