- Home /
The question is answered, right answer was accepted
Defining a script that is attached to gameobject ? Something like public C#Script
Hello everyone, I have a very little question. I have some gameobjects which can be interacted by player. Let's assume there 4 of them in once scene. All of them have same script ObjectInteraction.cs which will be enabled by player later on. ( when near & key is pressed ) But all of these objects need to start a different action, and I want to write these actions in different scripts. Like, StartAnimation.cs, StartMovement.cs, InstantiateObject.cs, DestroyObject.cs . What I want to do in ObjectInteraction script is something like this :
public Script action; // by Script, I mean whatever will be dragged & dropped on the inspector, can it be done ?
void OnEnable(){
//some stuff
action.enabled = true;
}
In other words, I want the gameObject to activate a script that is attached to it, but that script will be determined by dragging & dropping on the inspector. Can it be done ? I mean I know I can create different functions and bools in ObjectInteraction.cs to enable different scripts, but there will be a lot of workload since I consider creating a lot of different actions. Any ideas ? Thanks.
so you want to be able to define the objectInteraction script via the inspector depending on the game object, but each different object still define the same interaction script in inspector? I just want to make your question clear.
Look at the images below, you see I have 2 game objects, both have the same Interaction scripts, but each of them will enable different scripts in Interaction script. One will enable HaveObject, the other one will enable DropObject. I need a variable in Interaction script which can be differed from object to object, and ofcourse the type must be a c# script.
Answer by Baste · Feb 02, 2015 at 10:53 PM
public MonoBehaviour action;
Be wary about the variable name "action", though, as there's a build in C# class named Action.
Addendum: it might be that your problem is better solved by using delegates, though, in which case the Action class is actually something you might use. Read about it here.
I tried that one, but I was unable to drag and drop a script to inspector, it does not accept it. Is there anything I need to do in order to usage this ? Like moving folders or something like that.
using public $$anonymous$$onobehaviour variable means that it will be wanting a $$anonymous$$onobehaviour Script and public will allow it to be accessed in the inspecter, as @Baste said above. That is your best bet for this question.
But I can not drag and drop scripts onto it. By $$anonymous$$onobehaviour script, don't you mean
public class newClass : $$anonymous$$onoBehaviour {}
a script whose class is defined like that ?
Right, so you can't just directly drag the $$anonymous$$onoBehaviour script onto the object. You need an object (or prefab) that the behavior is attached to. In essence, $$anonymous$$onoBehaviours doesn't make any sense if they're not attached to a GameObject, as they have a defined gameObject, transform, and so on that must exist.
If that's not what you're looking for, what you need is a ScriptableObject, which is a class that allows you to define objects that works within the unity framework without any attached GameObjects. Check them out here. An example of where those would be useful is for stuff like items or spells in an RPG.