Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_CeoQ2dGMY_dd0g · May 08, 2019 at 08:30 PM · inventoryinventory system

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_CeoQ2dGMY_dd0g · May 09, 2019 at 07:35 PM 0
Share

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"

avatar image Cornelis-de-Jager unity_CeoQ2dGMY_dd0g · May 09, 2019 at 09:30 PM 0
Share

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.
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges