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 zombience · Sep 20, 2012 at 04:54 PM · c#gameobjectinstantiateaddcomponentattach

Can I use AddComponent to add (this.script) to an object?

So, I'm not sure if my syntax is incorrect or if I am trying to do something that can't be done. I have a lot of scripts talking to each other, so in order to try to keep organized I've decided to let scripts run everything. To preserve communication, I'm instantiating scripts instead of GameObjects, and then having the script create it's own game object and add itself to that object. I know that the usual is to instantiate a gameobject with scripts on it, but it would be easier for all my cross-referencing and troubleshooting if I could have scripts be instantiated, and have the gameObject created within that script.

So, here's the code where I'm stuck:

 public void selfInstantiate(GameObject obj, Vector3 pos, string ID ){
         
         GameObject flockFocusPoint = (GameObject)Instantiate(obj, pos, Quaternion.identity);
         flockFocusPoint.name = ID;
         obj.AddComponent(typeof(FlockAttractor));//this is where i'm not doing it right

         
         flockID = ID;
         FlockManager.flockIDs.Add(this);
         directionalForce();
     }


I am attempting to do something like:

obj.AddComponent < myScriptType > (this);

As in, I want this game object in the scene to have a direct reference to this individual script. I know that's incorrect syntax, but you get the idea. I also know that this is a bit backwards from how it's normally done (or how I've seen it done so far) but this would help me immensely in my organization if it were possible.

Thanks 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 Khada · Sep 20, 2012 at 05:35 PM 0
Share

You're just going to end up with loads of game objects as Instantiate will stick your script onto one (From what I know). It's a really backwards way to do things and will lead to massive headaches sooner or later. Perhaps share the reason for wanting a system like this and we can advise how to properly handle that?

EDIT: To be clear, instantiate means your script is going to exist on a game object, not the other way around. There's no reason to do it any other way.

avatar image zombience · Sep 20, 2012 at 05:44 PM 0
Share

I'm working with a flocking algorithm that is instantiating multiple flocks, and many objects per flock. so i have one main AllFlocks$$anonymous$$anager, an individual Flock$$anonymous$$anager, a manager of attractionPoints (the point the flock is attracted to), a UserPoints$$anonymous$$anager (players appear in the scene and points leave the flock to be attracted to the player). So, I need to be able to make references to which object is owned by which attractor, and which attractor is closest to which Player, and where they are. So far I've created a dictionary that has a direct reference to each attractor object through it's instance name, and also a separate List that represents the scripts attached to all those game objects so that I can get ID info, state info, etc.

I just figured with this much cross referencing, making a List that managed all the scripts that would then create their own objects and be associated with them would be far easier. This is also the first time I've done something so organizationally complex, so I'm trying to find the best way.

1 Reply

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

Answer by kmeboe · Sep 20, 2012 at 05:48 PM

Looks like the problem is that AddComponent attaches a new script, whereas you want to attach "this". What about changing your code around a tiny bit, using a static class method:

 public static void selfInstantiate(GameObject obj, Vector3 pos, string ID ){
 
        GameObject flockFocusPoint = (GameObject)Instantiate(obj, pos, Quaternion.identity);
        flockFocusPoint.name = ID;
        var thisScript = obj.AddComponent(typeof(FlockAttractor));
 
        thisScript.flockID = ID;
        FlockManager.flockIDs.Add(thisScript);
        thisScript.directionalForce();
     }
 

This way you're only creating the script once. Does that work for your desired model?

-Kevin

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 kmeboe · Sep 20, 2012 at 05:54 PM 0
Share

updated..."flockID" should probably be "thisScript.flockID". There may be other spots I didn't get quite right -- the compiler should tell you.

avatar image zombience · Sep 20, 2012 at 06:03 PM 0
Share

thanks for the tip on setting up the function as static. Hmm, it might work, but I think I'm in deep enough that I'll just stick to the way I was already doing it. Otherwise I'm afraid I'll break the organization and spend half a day just recovering my work.

But, that's a good idea for starting out next time

avatar image kmeboe · Sep 20, 2012 at 06:07 PM 0
Share

I know what you mean. Good luck figuring out your issue, and let us know if you find a way.

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

11 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

Related Questions

C# Why do I have to use .AddComponent and not "new (ClassName)"? 1 Answer

C# GameObjects Instantiate Into Each Other Issue 1 Answer

Player instantiates Backwords 0 Answers

FPS Object-on-wall Placement 0 Answers

Multiple Cars not working 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