- Home /
Assign script into another script in Inspector
//// In Action.cs
using UnityEngine;
using System.Collections;
public class Action : Object {
public string mTest;
}
//// In Actor.cs
using UnityEngine;
using System.Collections;
public class Actor : MonoBehaviour {
public Action mAction;
}
//// In ActorOnInspector.cs
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Actor))]
class ActorOnInspector : Editor {
SerializedObject actor;
SerializedProperty action;
void OnEnable ()
{
actor = new SerializedObject(target);
action = actor.FindProperty("mAction");
}
public override void OnInspectorGUI () {
Actor editing = target as Actor;
Object before = editing.mAction;
EditorGUILayout.PropertyField(action);
if (action.objectReferenceValue as Action != before)
editing.mAction = action.objectReferenceValue as Action;
actor.ApplyModifiedProperties();
}
}
Could anyone please tell me why I can't drag Action.cs(In Project window) to Actor::Action property field(In Inspector window)? Thank you.
Answer by slkjdfv · Jul 27, 2011 at 06:56 AM
You can't just call a script or assign a script to another script. If you want to call a script and edit it in another script it has to come from an object. Use this as an example to get a script from an object (C#). P.S. referencing a script from inside a function makes that variable only work in that function.
Using UnityEngine;
Using System.Generic;
void Update()
{
GameObject player = GameObject.Find("Player");
scriptOnPlayer plrScript = player.GetComponent<scriptOnPlayer>();
}
I signed in just to give you a vote and also a point, your code helped me!. I was stuck on this assigning thing for 2 days couldn't find stuff on this and plus I don't know program$$anonymous$$g which made it harder. Thank you for your help Good sir, have a Good day stay frosty! -KGH
Answer by testure · Jul 27, 2011 at 06:46 AM
You can't assign a script to a variable. Simple as that.
If I declare Actor.mAction as Object not Action, then I can drag Action.cs to it. But it can't cast back to Action type. Anything thought?