Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sparda928 · Nov 16, 2013 at 03:02 AM · imagereferenceclasses

Have two scripts modify eachother

I have two scripts - an inventory script attached to my player and an item database (which is giving me trouble). The database stores icons and models. I want to be able to instantiate the database object in my inventory script and pull up icons and models from the instance. How do I go about this?

The problem is that instantiating the database with a constructor seems to circumvent the Start() method and the public fields of the script I fill in the editor. So I end up with all my images and models being null. I could have the database build itself when the constructor is called but dragged-in references to my files don't seem to stick with the instance and also end up being null when I hit play. I tried using Resources.Load in my constructor and even though everything worked Unity threw angry errors about main threads and Load calls - I'd like to avoid those if I could.

So what is the right way of doing this?

Comment
Add comment · Show 3
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 Sericet1 · Nov 16, 2013 at 03:36 AM 0
Share

Can I see the scripts?

avatar image sparda928 · Nov 16, 2013 at 09:30 AM 0
Share

There's really not much to show - this is more of a design issue but here is an abridged version:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class ItemDatabase : $$anonymous$$onoBehaviour
 {
     public Texture2D icon;
 
         public Texture2D getIcon()
         {
             return icon;
         }
 }

And here is the other one:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Inventory : $$anonymous$$onoBehaviour
 {
         //now this is where I don't know what to do...
 
         //the following line wont work because I need an instance to call a method from ItemDatabase, and I haven't instantiated it
       public Texture2D itemIcon = ItemDatabase.getIcon();
 
         //ok, so lets instantiate it...
         public ItemDatabase itemDB = new ItemDatabase();
 
        //but now I need to make a constructor in ItemDatabase and I don't know how to tell it to use what I dragged into the icon field of the ItemDatabase script.
 }
avatar image VioKyma · Nov 16, 2013 at 10:30 AM 0
Share

So you want to have the ItemDatabase created as a Singleton, which is also a $$anonymous$$onoBehaviour that you can attach to a game object and add all the Textures etc in through the editor interface.

In this way, there is only ever one copy of the database, and you can reference it directly like so:

 public Texture2D itemIcon = ItemDatabase.Instance.getIcon();

If you're not sure what I'm talking about, you can check out these links:

  • Blog Post on Singleton

  • Singleton on Answers

  • Unity Wiki

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparda928 · Nov 16, 2013 at 11:52 PM

Ok so the another solution aside from what VioKyma wrote (which is awesome by the way) was to remove any and all constructors (general rule for all MonoBehaviours). Then have the ItemDatabase class build its database using Resources.Load in the Awake() method (which is always called before the Start() method). Then just drag the ItemDatabase script attached to the same object into the editor's ItemDatabase variable field (which you need to publicly define in the Inventory class). This ensures that before Start() is called in Inventory the database has already been built...works like a charm.

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Best practice for assigning class reference at runtime? 0 Answers

Referencing a script works... but not on iOS 0 Answers

Getting NullReferenceException? info inside 1 Answer

How do I reference variables from other class files? 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