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 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
Add comment · 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!

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'.

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

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.

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. $$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

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

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

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


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