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 /
  • Help Room /
avatar image
1
Question by gorevan · Dec 14, 2016 at 10:58 PM · automaticallyauto

How to auto assign a script within a script?

Hi there,

I have made a script which spawns enemies and I have a Music Control script that I wanting to attach via inspector however it will not let me add the music control to the prefab so it is auto assigned when the enemies spawn. I have tried making an editor script which someone helped me on here before with and put

if((messaboutEnemyScript.musicSystem == null) && (GameObject.FindWithTag("Music Control") != null)) { messaboutEnemyScript.musicSystem = GameObject.FindWithTag("Music Control"); }

However I get the message "Cannot implicitly convert type 'UnityEngine.GameObject' to 'MusicControl'" of course as you cannot do that. Does anyone know how I would achieve auto assigning this?

Thank you so much for all of your help recently! @Mavina ,

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 TBruce · Dec 15, 2016 at 01:13 AM 0
Share

Hi @gorevan,

Please send me all scripts involved

  1. The script that does the spawning

  2. The $$anonymous$$usic Control script

  3. Any enemy scripts if it applies

Explain in detail what you want.

So far as I understand it, you would like a $$anonymous$$onoBehavior script (the $$anonymous$$usic Control script) to be attached immediately when an enemy is spawned (correct me if I am wrong).

I am also assu$$anonymous$$g the enemy is spawned at run time,

You can use pastebin.com to create links for each script.

2 Replies

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

Answer by TBruce · Dec 15, 2016 at 02:12 AM

From the error you posted, it looks like you are trying to assign a game object to a MusicControl. If that is all that the problem is try the following first to fix the error.

In place of your original code

 if((messaboutEnemyScript.musicSystem == null) &&
     (GameObject.FindWithTag("Music Control") != null)) 
 {
     messaboutEnemyScript.musicSystem = GameObject.FindWithTag("Music Control");
 }

do this instead

 if((messaboutEnemyScript.musicSystem == null) &&
     (GameObject.FindWithTag("Music Control") != null) &&
     (GameObject.FindWithTag("Music Control").GetComponent<MusicControl>() != null)) 
 {
     messaboutEnemyScript.musicSystem = GameObject.FindWithTag("Music Control").GetComponent<MusicControl>();
 }

If you are still having problems please post all offending code as requested above.

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 gorevan · Dec 15, 2016 at 12:31 PM 0
Share

Works perfectly, thank you very much!

avatar image
0

Answer by Max_Bol · Dec 15, 2016 at 01:12 AM

We lack a bit of information for helping you out.

First, just for the case of being able to read your code, always put your code in coder tags. Like this :

         if ((messaboutEnemyScript.musicSystem == null) 
         && (GameObject.FindWithTag ("Music Control") != null)) {
         messaboutEnemyScript.musicSystem = GameObject.FindWithTag ("Music Control");
     }

That "101010" button is there for that purpose.

Now, the main problem we have at understanding your question is:

"What has to be attached to who?"

Should the Music Control script be attached to the Game Object that has the script which spawns the enemies or to the spawned enemies themselves?

Also, what is the messaboutEnemyScript referring to? We need the whole script to make sure we can help you out there. We can't just "guess" out of so little.

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

84 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

Related Questions

Auto generated blocks turning invisable? 2d game 1 Answer

Change scene automatically when all objects are clicked 0 Answers

Unity 5 constantly marking scenes as changed 0 Answers

I need to disable a gameobject in the hierarchy for 1 sec then it will automatically enable with OnClick on a UI Button in C#? Can you solve this miracle ASAP? 0 Answers

Lighting is different in build compared to editor? (Auto vs manual build) 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