Adding a Script to a GameObject from another GameObject

Greetings

I tried now for hours to find out how this can be done, but I still couldn’t manage.

I want to add a Script to my gameObject(Inventory) from another gameObject(Item). The problem is I will not know which script is attached to the Item gameObject.

I know it will be of the script type Weapon, but for weapon I have subclasses (like sword, staff etc.)

So how do I get the actual script (like sword) on to my inventory game object?

I tried the following:

_inventory.gameObject.AddComponent(_weapon.gameObject.GetComponent(typeof(Weapon)).ToString());
Debug.Log(_weapon.gameObject.GetComponent(typeof(Weapon)).ToString());

where _inventory and _weapon are both Transforms.

The Debug tells me that I don’t actually just get the name of the Script of the Weapon (like Sword or Staff) but I get the gameObjects name with the script in parenthesis:

Debug Log: Weapon1(Sword)

Instead i wanted to get just “Sword”.

Why is the gameObjects name attached to it?

Any help on how to only get the name “Sword” would be appreciated.

Make a base class function an override in all subclasses. Have it called GetName() or something like that and return the name of it. This may not be the prettiest method, but I think it will work. Maybe someone else with more unity experience can give you a way to do what you want, but that should work in the meantime.

I tried that and it does work, but as you said It is not really “pretty” and I was wondering if there is not a more direct method.

Also how can you “transport” a script from one class to another? So it would keep all its variables the same?

Or would you need to remove that component and before create a copy of the script (so creating a method which copies all the variables and puts them in the newly generated script) or is there also a better solution for this?

But thank you for the input, it certainly is a functioning workaround :wink:

Alright I couldn’t find any attach existing method, but I have a crazy idea. Mind you I have yet to code anything with unity, but let’s see if this works. The component has a member variable gameObject. So see what happens when you set your created scripts gameObject to who you want to “attach” it to.

why don’t you define a base class with a method implemented in it, for example:
=UNTESTED, obviously=

public void EnableScript(GameObject targetObject, string scriptName){
	targetObject.GetComponent<scriptName>().enabled = true;
}

this way you can use it in the other derived classes.

edit:
this way you must already have the script on your targetObject, but it’ll be disabled.

I was considering suggesting something like that, but I don’t really like that solution. Let’s say you have a lot of items, all with different stats. You would need to have every single variation of the script attached. This may be acceptable at first, but then say you add a system to randomize the items that spawn. So you instantiate a script randomize the variables to create a custom item. There is no way to preattach that specific script to the object.

I would think there should be some method to detach/attach items without destroying them. This would make it easy to assign items and pass them between objects on the fly. Of course there probably is a round about way to do it. Attach a script, then in the script make an assignment operator. Then after you attach the script copy the values from the spwaned script to the attached one. That would probably work, but I still don’t like it. You already created a new script, now you have to create another new script, then delete the old one. It’s just silly, you’ve now had to call new 2 times instead of just once and assigning it to wherever you want it.

With that, I am curious what happens when you assign the gameObject variable in the script. I still think that may solve this problem if you’re able to manually set it.

according to : http://unity3d.com/support/documentation/ScriptReference/Component.html

more specifically this: http://unity3d.com/support/documentation/ScriptReference/Component-gameObject.html

Component[/COLOR].gameObject [/B]
var gameObject : GameObject
Description
The game object this component is attached to. A component is always attached to a game object
I couldn’t find anything in GameObject that was exposed in the documenation that sounded like it was a list of all attached components. If someone had the source for unity they could obviously take a peak and let us know how it works, but I wouldn’t hold my breath.

Thank you for all the ideas.

I tried that but the scrip.gameObject is unfortunately read only… So you can’t reassign a new gameObject to a script. I was thinking to create a separate GameObject which holds just that script and passing the gameObject to the other gameObject (as a child) but this seems just all too complicated for such a simple thing.

If anyone has a better idea then just copying the script and destroying the old one, i would really appreciate it.