• 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 mechanicarts · Jun 03, 2018 at 12:32 AM · addcomponent

Script component gets attached multiple times

Hello. I'm trying to attach a script to 3 different gameObjects that lie in a list. However, the script gets attached many times (18 to be precise, always the same and always on all 3 objects). Obviously this is undesirable behavior.

I'm adding my variables for ease, they get changed in runtime, but I've double checked their values using logs, so the numbers I'm giving are 100% certain.
My code:

 Variable values in text:
 RPosArray.Count = 3
 randomSpawn = null
 royalArray = array of 3 gameObjects
 randomSpawnTraverse = 2 (RPosArray -1)
 royaltyArray = empty
 card = 0
 
         foreach (GameObject RPos in RPosArray)
         {
             randomSpawn = RPosArray[randomSpawnTraverse].gameObject;
             GameObject newRCard = Instantiate(royalArray[card], randomSpawn.transform.position, randomSpawn.transform.rotation);
             newRCard.AddComponent<EnemyHealth>();
             royaltyArray.Add(newRCard);
             newRCard = null;
             randomSpawnTraverse--;
             card++;
         }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Serellyn · Jun 03, 2018 at 09:14 AM

Okay, you can probably detect it quite easily if the problem lies in your foreach. Place a Debug.Log with a counter in your foreach, run your game and see what it outputs. If it outputs 1,2,3,....17,18 you will know that your Array is not what you wanted. If it ouputs 1,1,1,1 2,2,2,2, 3,3,3,3, etc or something like that, your foreach gets called multiple times.


If i read your problem, I suspect the last option. You might have attached your script to a gameobject and have multiple instances of this gameobject, then this might happen.

Comment
Add comment · Show 3 · 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 mechanicarts · Jun 03, 2018 at 10:40 AM 0
Share

There are only 3 gameObjects in the array, and only 1 instance of each. I checked the hierarchy during play, and it seems exactly how it should be.
I don't understand how the counter would output 1 four times, but I'm just gonna try that. EDIT: Oddly enough, my counter only prints 0,1,2 (starts at 0).

avatar image Serellyn mechanicarts · Jun 03, 2018 at 12:39 PM 0
Share

Okay, so printing only 0,1,2 is great, those are your 3 GameObjects. Currently I don't have any more ideas what the problem could be. If this is little issue is holding you back I wouldn't $$anonymous$$d taking a quick look at your project as a whole, to see where the problem occurs. If you'd like that you could send me a pm on the forums with a download link. https://forum.unity.com/members/serellyn.65929/

avatar image mechanicarts Serellyn · Jun 03, 2018 at 12:58 PM 0
Share

Ha! I figured the problem as I was going through the project files. Somehow (perhaps at a previous iteration) the prefabs had 17 instances of my script attached, making it a total 18 at runtime. It works now, thanks for your help!

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

83 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

Related Questions

Loading Scripts onto GO before in-game scene 1 Answer

Adding Prefab Components 1 Answer

AddComponent in script modifying Prefab Assets? 0 Answers

AddComponent() creating two colliders? 0 Answers

AddComponent() is adding 64 instances of my script to my GO 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