- Home /
GO.AddComponent just adds an empty Script
Hi there,
i created this Script to ask for a specific Script inside a GameObject. if it doesn´t exist, a Button should appear to create the Script in the GO.
The GetComponent works, the button works - but the AddComponent not...
This is the Script: using UnityEngine; using UnityEditor; using System.Collections;
public class Sys_EventList : EditorWindow
{
GUISkin new_skin;
Vector2 scrollPos;
Sys_EventRun eventscript;
// Add menu item named "My Window" to the Window menu
[MenuItem("RPG System/Event List")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(Sys_EventList), false, "EventList");
}
void OnGUI()
{
new_skin = Resources.Load("GUISkin_Event_System") as GUISkin;
GUILayout.Label("Event Liste", EditorStyles.boldLabel);
EditorGUILayout.HelpBox("Click on a Event Command to open its settings or delete it.", MessageType.Info, true);
eventscript = null;
if(Selection.activeGameObject != null){eventscript = Selection.activeGameObject.GetComponent<Sys_EventRun>();}
if(eventscript == null){
EditorGUILayout.HelpBox("Add the Sys_EventRun Script to your GameObject to activate the Event System for it!", MessageType.Warning, true);
if(GUILayout.Button("ADD Sys_EventRun to Object")){
Selection.activeGameObject.AddComponent<Sys_EventRun>();
}
}
}
}
The only thing that happens is, that an empty Component named "Script" is attached to the GO... Instead of my Script ._. What is wrong here?
I think that you need to store a reference to that script and use AddComponent to add that reference. Create a public variable that contains that script (drag&drop in the editor) then try AddComponent, otherwise it will not know where to get the script from and simply create an empty script for you.
I tested your code and it works for me, so try changing the script that you want to add to the gameobject for another one and see if you get the same problem, also try commenting out the new_skin line and see if that works
I've no problem with GetComponent (as i wrote - GetComponent - works!)
I've a problem with AddComponent!
Ins$$anonymous$$d of Adding my Sys_EventRun.cs to the GameObject, it adds a new empty Script called 'Script' So the inspector shows as components the '$$anonymous$$esh renderer', 'Rigidbody', 'Script' But ins$$anonymous$$d of 'Script', it should add 'Sys_EventRun'.
But this should be an editor extention - without the need to assign a public variable via inspector or such first... That's why i want a hardcoded system.
Answer by Oana · Jul 02, 2013 at 11:59 AM
A few quick checks:
Is the script a MonoBehavior?
Can you drag it on an object by hand without a problem?
Are they in the same package or do you need a "using" statement?
yes it's a $$anonymous$$onoBehavior (language C#) i tried with a complete new created script - even this is not added.
yes i can drag them to an object without problems (but the button way is more userfriendly - especially in greater projects)
the script is in the same package.
What about adding a different script with that code? Does that work? Also, what does your Sys_EventRun script look like? Does it use any weird stuff in it?
The Sys_EventRun is a Struct interpreter. $$anonymous$$y whole system is something like the rpg-maker Event System (but with less commands).
It holds a public list struct wich stores event informations (like command strings, framecounts, animation names..etc)
It runs through the list, and starts one action after another (when the previous action is done).
so - the script holds a list, and a function with many if consitions to ask for the list contents. nothing weird.
i tried to add a newly created script (create -> script c#) - not working.
Your answer
Follow this Question
Related Questions
How to add new instance of a script to a game object? 2 Answers
Can I use AddComponent to add (this.script) to an object? 1 Answer
Modifying several gameobjects with an editor script? 2 Answers
How would you override Editor object dragging? 0 Answers
Using Collider.OnCollisionEnter(Collision) without attaching the script to the concerned GameObject 2 Answers