• 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 akyang · Jul 27, 2011 at 05:41 AM · editorinspectorclassserializeoninspectorgui

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.

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

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>();    
 }
Comment
Ben-Stoneman
ciwiaf
Noydj
KrishGameHacker

People who like this

4 Show 1 · 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 KrishGameHacker · Nov 16, 2020 at 11:07 PM 0
Share

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 programming which made it harder. Thank you for your help Good sir, have a Good day stay frosty! -KGH

avatar image

Answer by testure · Jul 27, 2011 at 06:46 AM

You can't assign a script to a variable. Simple as that.

Comment
thekoop
Ben-Stoneman

People who like this

0 Show 1 · 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 akyang · Jul 28, 2011 at 01:44 AM 0
Share

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?

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to serialize an array of classes 1 Answer

Default Serialization Values Not Being Set 0 Answers

OnInspectorGUI - Using the default Object Selection popup. 1 Answer

How do you make a custom inspector for a class or instance? 3 Answers

Inspector button for custom class 1 Answer


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