- Home /
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
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.
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.
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
updated..."flockID" should probably be "thisScript.flockID". There may be other spots I didn't get quite right -- the compiler should tell you.
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
I know what you mean. Good luck figuring out your issue, and let us know if you find a way.
Your answer
Follow this Question
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