• 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 leotolentino · Jul 24, 2014 at 08:21 PM · gameobjectprefab

Acessing scripts in other game objects with prefabs, without using GameObject.Find

Hi everyone.

I'm trying to learn more about scripting in Unity and I'm coming across a problem.

I have a small text-based game, and I'm trying to achieve this result:

1) Click a button to spawn a bee. 2) After 2 seconds(this time can be variable), a bee adds nectar to the hive.

The first part that I had with this, was assigning a game object that I called a "resourceBank" to my bee, which is a prefab. I'm not able to set it up without using a GameObject.Find, and having it access it through my game controller.\\

There is a method in resourceBank that called addNectar, adds nectar to itself, and keeps track of how much collected nectar is in the hive. I am accessing this method by way of the bee, and in the future, it can perform other functions such as buildWax, addPollen, and all that.

I was wondering if I am approaching this the correct way, am I limited to using GameObject.Find, I've heard it would be slow, and am planning to spawn several bees as this game progresses.

Comment
Add comment · Show 1
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 AndyMartin458 · Jul 24, 2014 at 08:21 PM 1
Share

You can assign the reference in the inspector before you ever run the game.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by smallbit · Jul 25, 2014 at 02:06 AM

Firstly Find is slow but if you are going to use it every now and then only its acceptable.

In your case you could use singleton code pattern for your hive. Singleton can be used for class that can only have one instance (like your hive i assume). Than you can access it from anyplace in your code without the reference to the actual gameobject on which the script is.

For example your hive script code

  public class Hive : MonoBehaviour
     {
     //declare instance
     public static Hive instance; 
     
     void Start()
     {
     //very important to initialize your instance
        instance = this;
     }
     
     // must be public to be accessible 
     public void AddNectar(int amount)
     {
     //your code
     }
 
 
  //you can also use static method (this can operate only on static members)
 public static void AddPollen(int amount)
 {
  
 
 }
 

than in your bee script you just call like this ( no need to know the game object, so no need to use find)

   Hive.instance.AddNectar(20); need to call via instance
     Hive.AddPollen(30); //method is static no need to call instance

 

I suggest you google about singleton pattern (and static methods) its a very simple to use and can save a lot of time !!! Since i started using it my life is much easier :)

Comment
Add comment · Show 1 · 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 leotolentino · Jul 25, 2014 at 09:57 PM 0
Share

I'm pretty new to this and I've never heard of singleton code patterns, I will look this up. Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Two exact objects behaving differently... *scratches head* 0 Answers

How do I assign a script to an instantiated prefab? 2 Answers

it is not possible to invoke an expression of type int 2 Answers

How to create GameObject without adding it to scene? 1 Answer

Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 0 Answers


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