• 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
Question by Chaosgod_Esper · Jul 02, 2013 at 08:19 AM · gameobjecteditoraddcomponentattach

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?

Comment

People who like this

0 Show 4
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 Eugenius · Jul 02, 2013 at 09:02 AM 0
Share

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.

avatar image markpdolby · Jul 02, 2013 at 09:19 AM 0
Share

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

avatar image Chaosgod_Esper · Jul 02, 2013 at 10:19 AM 0
Share

I've no problem with GetComponent (as i wrote - GetComponent - works!)

I've a problem with AddComponent!

Instead of Adding my Sys_EventRun.cs to the GameObject, it adds a new empty Script called 'Script' So the inspector shows as components the 'Mesh renderer', 'Rigidbody', 'Script' But instead of 'Script', it should add 'Sys_EventRun'.

avatar image Chaosgod_Esper · Jul 02, 2013 at 10:57 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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?

Comment
Bunny83
Chaosgod_Esper

People who like this

2 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 Chaosgod_Esper · Jul 02, 2013 at 12:11 PM 0
Share

yes it's a MonoBehavior (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.

avatar image Oana · Jul 02, 2013 at 01:02 PM 0
Share

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?

avatar image Chaosgod_Esper · Jul 02, 2013 at 01:35 PM 0
Share

The Sys_EventRun is a Struct interpreter. My 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add new instance of a script to a game object? 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

Show prefab/GO in viewport via script 1 Answer

keep gameObject in hierarchy folded when not selected 3 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