- Home /
Attach a script to another script problems C#
I'm have a couple weird problems with attaching scripts inside other scripts.
I have a game object on the stage with the following script.
using UnityEngine; using System.Collections;
public class GameManager : MonoBehaviour {
//Managers
public LevelManager levelManager;
}
And I created the other script which looks like the following
using UnityEngine; using System.Collections;
public class LevelManager : MonoBehaviour {
}
Now when I try to drag and drop the script onto the spot in the scripts variables in the unity GUI it wont attach. Also when I click the little browse circle it wont show up in the list.
I've tried it with multiple scripts and I'm having the same problem.
This has me beat. Is their an article somewhere I can read that goes over including other scripts in c#. I've had a search but can't find anything. I must be doing something wrong.
Answer by pyro · Nov 30, 2010 at 02:35 PM
You need to attach the script to an object in the scene first, then drag that OBJECT into your other script.. not the script file itself.
This worked but I remember not having to attach the script to a Game Object? or has it always been like this?
Would it be better if I just attach the script as a component to the Game$$anonymous$$anager GameObject?
yes think of the script file as the class definition, and when you drag it onto a gameobject it actually instantiates the class in memory it doesnt really matter where you attach the component, its personal preference. I generally like to keep separate components on separate gameobjects
Answer by Maarten · Nov 30, 2010 at 11:35 AM
Please make sure the LevelManager class has the attribute [Serializable]. Otherwise it wont show in the inspector.
The script shows in the inspector I just cant drag and drop the script on it.
Answer by ckfinite · Nov 30, 2010 at 02:27 PM
I cannot find a problem in any of your code. I have done just this sort of thing hundreds of times in my project, and it works. Try right clicking on any asset, and selecting "reimport all". I find that fixes a lot.
Your answer
Follow this Question
Related Questions
Create an object and assign code to it via script? 1 Answer
scripts as a addcomponent to a gameobject 2 Answers
unable to attach a script to a prefab 1 Answer
Can't Add Script 1 Answer