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 /
avatar image
1
Question by centaurianmudpig · Oct 02, 2011 at 10:42 PM · instantiatearrayobject

Instantiate duplicates script in subsequent Objects

I have a simple C# function which adds a script to an Instantiated Game Object and then stores it in an array.

     public void AddPort(int _group) {            
     //Create a new GunPort with GunPort Script attached
     group[_group].Add(Instantiate(gameObject.AddComponent<GunPort>()) as GunPort);
     group[_group][group[_group].Count-1].name = ("GunPort " + _group);
     
     //Initialise the GunPort
     group[_group][group[_group].Count-1].Init();
 }

     public void Init(Transform _owner, int _maxGroups) {    
     owner = _owner;
     
     //Initialise the group array to contain the GunPort's
     group = new List<GunPort>[_maxGroups];
     for(int i = 0; i < _maxGroups; i++) {
         group[i] = new List<GunPort>();
     }
 }

The problem I am having is that each subsequent script created will have n+1 GunPort scripts attached (where n is the number of GunPorts already created and stored in the array).

So, when I create 4 GunPorts ...

 //..Called inside main function
         AddPort(1);  //1 GunPort script attached, OK
     AddPort(1);  //2 GunPort scripts attached, should be 1
     AddPort(2);  //3 GunPort scripts attached, should be 1
     AddPort(2);  //4 GunPort scripts attached, should be 1

What am I doing wrong?

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 Jushik · Dec 16, 2018 at 07:09 AM 0
Share

Wow! 2018 year now. And I got this problem.

 for (int i = 0; i < 3; i++)
         {
             /*here string path choosed according to some behavoirs*/
 
             var newObject = Instantiate(Resources.Load<GameObject>(path));
             var script = newObject.AddComponent<Hole_Color>();
             newObject.transform.SetParent(transform, false);
         }

Result: 1 game object - 1 script; 2 game object - 2 scripts; 3 game object - 3 scripts. Cant find explanation of this.

2 Replies

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

Answer by aldonaletto · Oct 03, 2011 at 12:07 AM

The whole thing is very confusing, but I guess that the problem is in the gameObject.AddComponent<GunPort>() instruction: it returns a GunPort object, but at the same time adds a new GunPort to gameObject!
I honestly couldn't understand what exactly you're trying to do. Are you instantiating an empty game object with the GunPort script attached? If that's the idea, I suggest the following:

public void AddPort(int _group) {    
    // Create a new GameObject named "GunPort N", where N is number _group
    GameObject newGP = GameObject.GameObject("GunPort "+_group);  
    // attach the GunPort script to it and get a reference
    GunPort gpScript = newGP.AddComponent<GunPort>();
    // add the new GunPort object to group[_group]
    group[_group].Add(newGP);
    // Initialise the new GunPort
    gpScript.Init();
}
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 centaurianmudpig · Oct 03, 2011 at 12:49 AM 0
Share

Thats exactly what I am doing, thanks for the info. Though I have used the following code to create the game object and attach the script:

GunPort gunPort = (new GameObject()).AddComponent();

avatar image
0

Answer by Jushik · Dec 16, 2018 at 08:04 AM

Figured out!

 group[_group].Add(Instantiate(gameObject.AddComponent<GunPort>()) as GunPort);

Here you added GunPort component to a gameobject which is like a prefab for Instantiate. Next time when you call AddPort

 group[_group].Add(Instantiate(gameObject.AddComponent<GunPort>()) as GunPort);   //gameobject already has Gunport

it is instantiates that prefab with already added GunPort component and gameObject.AddComponent()) as GunPort); adds another GunPort. I think it should be like this:

 GameObject go = Instantiate(gameObject);
 go.AddComponent<GunPort>()) as GunPort;
 group[_group].Add(go);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Randomly instantiate objects from array without choosing the same item twice. 2 Answers

How do I name cloned objects inside a for-loop? 2 Answers

Add instantiate object to GameObject[] 2 Answers

Tagging object while instating not working. 3 Answers

[SOLVED]Object selection scrip doesn't work. 2 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