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. Dismiss Notice

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

Discussion in 'Scripting' started by Sumaccamus, Jul 18, 2014.

  1. Sumaccamus

    Sumaccamus

    Joined:
    Jul 5, 2014
    Posts:
    26
    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.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    1. Its called german. ;)
    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!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.

     
  5. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Its aswell about the basic concepts of programming he's missing.
     
  6. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    Use other.GetComponent<Inventory>().AddItem(1) instead to get the instance attached to the other object. You are trying to call a static method.