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 GregoryNeal · Apr 30, 2015 at 04:43 AM · script erroraddcomponent

AddComponent() is adding 64 instances of my script to my GO

I am using this code to instantiate one gameobject with a script Ground attached to it:

     public void SpawnInitial()
     {
         int loadDist = GetComponent<GlobalWorldValues>().loadDist;
         GameObject[] originals = Resources.LoadAll<GameObject>(midPath);
         GameObject original = originals[UnityEngine.Random.Range(0,originals.Length)];
         Vector3 pos = new Vector3(transform.parent.transform.position.x,-original.GetComponent<SpriteRenderer>().bounds.extents.y,0);
         Ground g = original.AddComponent<Ground>();
         print("added g to original");
         g.isReversed = false;
         g.isSloped = false;
         g.path = midPath + "/" + original.transform.name.Replace("(Clone)","");
         Instantiate(original,pos,Quaternion.identity);
     }

It's compiling just fine no errors. But when I hit play I'm met with this in the hierarchy:

alt text

It's adding 64 instances of this script to my object. I've counted and there are only 4 instances of AddComponent() to ANY object in every script that even has the ability to run this method. And none of them are in any kind of loop. Needless to say, I'm so confused, any insight would be appreciated. If you need more info just ask I'll be glad to post more. Thanks.

capture.jpg (96.0 kB)
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 HarshadK · Apr 30, 2015 at 05:38 AM

It might be happening because you are actually adding the component to the prefab itself which is loaded from Resources. I would say first Instantiate the gameobject and then add the component to it as below:

 public void SpawnInitial()
      {
          int loadDist = GetComponent<GlobalWorldValues>().loadDist;
          GameObject[] originals = Resources.LoadAll<GameObject>(midPath);
          GameObject original = originals[UnityEngine.Random.Range(0,originals.Length)];
          Vector3 pos = new Vector3(transform.parent.transform.position.x,-original.GetComponent<SpriteRenderer>().bounds.extents.y,0);
          GameObject clone = (GameObject) Instantiate(original,pos,Quaternion.identity);
          Ground g = clone.AddComponent<Ground>();
          print("added g to original");
          g.isReversed = false;
          g.isSloped = false;
          g.path = midPath + "/" + original.transform.name.Replace("(Clone)","");
      }

Comment
Add comment · Show 5 · 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 GregoryNeal · Apr 30, 2015 at 01:12 PM 0
Share

Unfortunately that doesn't work either. But it leads me to believe the error is elsewhere.

avatar image Bonfire-Boy · Apr 30, 2015 at 01:32 PM 0
Share

Can you confirm that the prefab has no Ground components to start with? If it's ended up with 63 then no code changes are going to work till you fix the prefab. To be sure I would protect the original.AddComponent... with a check like if (original.GetComponent... == null) (and log an error if the check fails).

avatar image GregoryNeal · Apr 30, 2015 at 01:42 PM 0
Share

Oh man, your comment led me to discover that I'm not doing what I wanted to do. There were a bunch of Ground scripts attached to the prefabs, but I don't want them to be attached to the prefab. I just want them to be attached to the instance when I spawn it, then destroy it when I'm done with it, recovering the Ground script data.

avatar image HarshadK · Apr 30, 2015 at 02:48 PM 0
Share

And I believe those 63 scripts were added when you ran your previous script (the one in the question) that was adding component directly to the prefab.

avatar image GregoryNeal · Apr 30, 2015 at 07:48 PM 0
Share

Yea I just gave up trying to add an instance of a $$anonymous$$onobehaviour to a prefab and just had them have it as a component by default

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

How to access Vive controllers in a script attached on a simple game object(a cube)? 0 Answers

Assets\Standard Assets\Characters\Third Person Character\Scripts\Third Person User Control.cs(42,52): error C S 0103: The name 'Control Mode' does not exist in the current context 0 Answers

Trying to child multiple GameObjects to one master object in script 1 Answer

How Do I Get a Script to Reference the GameObject When Using AddComponent? 1 Answer

Creating prefab through custom editor window 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