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 supermodular · Apr 02, 2012 at 05:49 PM · instantiatestartmanagerpointer

A question about instantiating.

I've got a game manager script in which I instantiate a prefab, call it "enemy". Now this enemy has a script that sometimes needs to ask various questions of the game manager, and so the enemy script includes a reference to the game manager. As I see it, there are a few ways I could handle this, all of which seem to have shortcomings:

a) Use Find to set up this pointer to the game manager in enemy's Start function, but this seems like overkill -- after all, the game manager just built the enemy, so you'd think the game manager should be able to let the enemy know how to find it.

b) Immediately after the game manager instantiates the enemy, have the game manager set the enemy's manager field. Here the problem is that it (sometimes) gives a run-time error, as the enemy may begin running before the game manager gets to set the enemy's manager field.

c) Adjust b) by having the enemy wait some amount of time before doing anything. This seems pretty ugly, since who knows how much time it actually needs to wait.

d) Adjust b) by having a "ready" field that the manager sets to true only when it has done the necessary initialization, and do a ready check before running the contents of Update. Or equivalently, only run the contents of Update if the manager field isn't null. But this check will now be run on every update, even though once it is true, it'll be true forever.

Is there a better/cleaner way to handle this? Or is this indicative of a structural problem -- maybe I should somehow set things up so enemy never needs access to the manager, and it is always the manager that talks to the enemy -- but I don't see how I'd do that either. Any suggestions? Thanks very much in advance.

Comment
Add comment · Show 2
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 Branx · Apr 02, 2012 at 06:23 PM 1
Share

You can find your game$$anonymous$$anager by:

  1. Object.FindObjectOfType(); (It's slow but you only need to call it once so why bother)

  2. Use Singleton as described here: http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html

avatar image supermodular · Apr 04, 2012 at 09:25 PM 0
Share

Thanks, I'll look into Singletons. Regarding your first suggestion, wouldn't I need to call some variant of Find once per instantiation? For enemies that isn't a big deal, but if I'm making lots of bullets this seems more problematic (I might want to do something like this if bullets drain health, say, so they'd need to talk to the unit that created them).

1 Reply

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

Answer by save · Apr 02, 2012 at 10:09 PM

If you use static variables and functions in the manager this shouldn't be an issue.

 //In the Enemy script
 enemyVariable = GameManager.someVariable;
 //or
 enemyVariable = GameManager.SomeFunction(passSomeVariableIn, andYetAnother);

 //In the Game Manager script
 static var someVariable : boolean = true;
 static function SomeFunction (someVariable : int, anotherVariable : int) : boolean {
     var returnThis : boolean = false;
     if (someVariable==anotherVariable) returnThis = true;
     return returnThis;
 }

Although sometimes you need to keep a reference to a non-static part of a script, then you could make one static variable for a series of objects and reference to it with GetComponent. You could make the GameManager reference to itself for a non-static variable or function (GameManager.gmScript.thePublicVariable) where gmScript is declared: static var gmScript : GameManager;. Then set in Awake(): gmScript = GetComponent(GameManager);.

Another way of solving it is by having a static variable in the multiple objects and then finding the component once if it's null, otherwise do nothing. This has next to none impact on initialization. Although, keeping a manager static is the easiest way to go.

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 supermodular · Apr 04, 2012 at 09:22 PM 0
Share

Thanks for the quick feedback! Unfortunately I'm still confused: how would my Enemy script know what Game$$anonymous$$anager is to begin with?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player starts instantiate at random places? 3 Answers

Preserve state of objects when stopping game 0 Answers

How to wait scene to load if using IEnumerator Start()? 0 Answers

Does transform get copied before or after Start() called inside Instantiate() 1 Answer

I'm attempting to instantiate a prefab GameObject, but it just won't show up!! 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