- Home /
sending information to a function in another script to use
So I have an inventory system set up in a game I'm building. I'm still a beginner and I've been using tutorials to help me build it. Though, the tutorial left me with questions I can't figure out how to solve on my own.
In my inventory script, I have a function that will give the player an item. It's a public void called "GiveItem()". In the bracket will be the item I wish to give the player.
It looks like this
public void GiveItem(int id)
{
Item itemToAdd = itemDatabase.GetItem(id); //Gab the id number from the separate Item data base script I have built
characterItems.Add(itemToAdd); //Add that id to the inventory
inventoryUI.AddNewItem(itemToAdd);
}
I need it so that when the player interacts with a specific object, the player will be given a specific object. I could put every interaction I need in the inventory script, knowing I'd work, but it'd be messy and difficult. So I want to create a separate script with the requirements. Like, if the player clicks on this box, then, give them the item. But I can't figure out how I'd be able to use the function in a different script.
I was hoping someone would have a solution.
Answer by Cornelis-de-Jager · May 08, 2019 at 10:19 PM
Referencing different scripts is actually quite easy - but can get confusing if you don't have a proper hierarchy and setup. Below I will discus methods and strategies to reference other scripts.
Method 1: Scripts on the Same Object
This is the easiest on to do. If you have to scripts on the same Transform/Object you can reference it with GetComponent(). For example you have two scripts on an object, InventoryItemsDatabase and InventoryControll. You want to access InventoryItemsDatabase from the InventoryControll script. It will look like this
public class InventoryControll : MonoBehaviour {
// Variable to store script so we
// don't have to use GetComponent every time.
InventoryItemsDatabase itemDB;
// Assign in on Start
void Start () {
itemDB = GetComponent<InventoryItemsDatabase>();
}
// Usage Example
void RemoveItem (int id){
itemDB.Remove(id);
}
}
Method 2: Referencing Objects from Parent Or Root
Say you have an hierarchy structure of objects
-> GameManager {Root Object}
--> PlayerManager
--> AIManager
--> InventoryManager [InventoryManager, InventoryDatabase] {Parent Object to InventoryControll}
---> InventoryControll [InventoryControll]
Here we have the InventoryControll on a separate object that is a child if the InventoryManager object which contains two scripts, InventoryManager and InventoryDatabase. If we want to access the InventoryDatabase we can use the following code:
// Assign in on Start
void Start () {
// Using GetComponent
itemDB = transform.parent.GetComponent<InventoryItemsDatabase>();
// Using GetComponentInParent
itemDB = GetComponentInParent<InventoryItemsDatabase>();
}
Similar if the object was in the root (The most top parent you can get) you can use the code:
transform.root.GetComponent<InventoryItemsDatabase>()
Method 3: Drop and Drag for non hierarchy structures
Another way to do it is to add a reference to the object, drop and drag that object onto a variable. Create a public
variable as a Transform.
public Transform InventoryDatabaseTransform;
This variable will now be visible in the inspector. Simply while you have the InventoryControll object select in the hierarchy (so we can see the above variable in the inspector), click and drag the InventoryDatabase object from the inspector to the variable in the inspector. You should now be able to use getcomponent in the InventoryControll script as such:
void Start () {
itemDB = InventoryDatabaseTransform.GetComponent<InventoryDatabase> ();
}
Hope this helped. Just comment if you have further issues.
Method 4: SearchObject
Simply search for the object you want by name:
void Start () {
itemDB = GameObject.Find("InventoryDatabase").GetComponent<InventoryDatabase>();
}
I hope this helped you out!
So I tried the third and the forth method because I felt either would work in my scenario. The only problem is I put in my ItemDB (Which in my scripts is called "ItemDataBase" And I get the error CS0118 "'ItemDataBase' is a type but is used like a variable"
For the Forth method, change the name the object to one that is different to the script name. So for example, your script is called ItemDataBase
. In your hierarchy name the object it is on ItemDataBase$$anonymous$$anager
Note that the UI, Terrian and player are just examples of objects in the Hierarchy View
-> UI
-> Terrain
-> ItemDataBase$$anonymous$$anager - [ItemDataBase.cs]
-> Player
In Your Script that wants to find the ItemDataBase it will look as follow:
void Start () {
itemDB = GameObject.Find("InventoryDataBase$$anonymous$$anager").GetComponent<InventoryDatabase>();
}
That should give you the correct answer. The issue you were having before is that you were assigning Type to a variable ins$$anonymous$$d of a value to variable. See below of quick explanation:
ItemDataBase ItemDB = GameObject.Find...
[Type] [Variable] = [Object or Value]
[Variable] = [Object or Value] ---> Correct
[Varaible] = [Type] ---> Wrong. Also what you did and why you got an error.
Answer by Temseii · May 08, 2019 at 08:44 PM
One way would be to create a script, give it to each box and assign them an id. Upon clicking the box, retrieve that id and call GiveItem passing in the id you retrieved.
Your answer
Follow this Question
Related Questions
Inventory code not working [JS] 0 Answers
Proper item usage design 0 Answers
How to save and load inventory? 1 Answer
Inventory loop bolt 0 Answers
Inventory question 1 Answer