Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kinkersnick · Oct 03, 2011 at 02:37 PM · editorinstantiatedynamic

Dynamically update the editor?

Hi,

I've been trying to figure this out for hours, been searching and testing to no avail.

What I want to do is have a script with variables which can be edited in the inspector, which affect the instantiation of prefabs at runtime in the Awake() or Start() function, but which runs and updates in the editor, changing as the variables are changed. Here is a very basic example script:

Code:

 @script ExecuteInEditMode;
 
 var cubePrefab : GameObject;
 var lineLength : float;
 var numberOfCubes : int;
 
 function Start() {
 var cubeStep : float = lineLength/numberOfCubes;
 for(var i = 0; i < numberOfCubes; i++) {
     var newCube : GameObject;
     newCube = Instantiate (cubePrefab, Vector3(i * cubeStep,0,0), Quaternion.identity);
     }
 }

So it instantiates a number of cubes equally along a given line length, very simple. With the ExecuteInEditMode, the script runs once, but then if you change the length of the line, or the number of cubes you want, it doesn't change. Is there a way to make this work? I realise it may have something to do with editor scripts, so I've been trying to get my head around them. I tried this:

Code:

 @CustomEditor (CubeLine)
 class CubeLineEditor extends Editor {
     function OnInspectorGUI () {
         if (GUI.changed)
             target.Awake();
     }
 }

But for one thing, it blocks out everything else in the inspector so you can no longer see the variables coming from the CubeLine.js script, and also when I did get something like this working, after you change the values, it just instantiates another set of cubes aswell as the ones already there. What I want is that, say you set the line length to 10, and number of cubes to 10, you'll see in the editor a line of ten cubes each spaced one point apart. But then if you change the length to 20 in the inspector, you'll see the line update to be 20 long, with a cube every 2 points apart etc. etc. What is the approach to such a thing?

Thank you so much, and hope this is clear. S

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BerggreenDK · Oct 03, 2011 at 02:38 PM

If you need access to variables through the inspector, then add the word "public" infront of var.

Then they will be accessable through the inspector.

Comment
Add comment · 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
0

Answer by kinkersnick · Oct 03, 2011 at 03:44 PM

I can access the variables fine under normal circumstances, but they disappear when I use an editor script - do you mean I need to specify the variables in the normal script AND in the editor script as well? That seems rather strange... But the main question I'm asking is how to make the editor update things dynamically such as number of instantiated prefabs etc. Basically how to rerun an Awake() function when the inspector is changed: not instantiate stuff on top of what is there, but refresh it anew.

Comment
Add comment · 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 Tseng · Oct 03, 2011 at 06:17 PM 0
Share

Don't reply as answer, use comments! This is no forum, its Q & A and the answers are sorted by the vote ups/accepted status!

avatar image
0

Answer by Tseng · Oct 03, 2011 at 06:31 PM

Well, first off you need to move this code into its own method, because you have to call it on update.

 function InstantiateCubes() {
     for(var i = 0; i < numberOfCubes; i++) {
         var newCube : GameObject;
         newCube = Instantiate (cubePrefab, Vector3(i * cubeStep,0,0), Quaternion.identity);
         }
     }
  }

And then call it every time, when a the input is changed, similar to the one you already pointed out

 if(GUI.changed)
     EditorUtility.SetDirty(target);

You shouldn't call Awake or Start methods directly from code. Of course, don't forget to remove the previous cubes before adding new ones. This will flag your target (the object you're editing) as dirty, which will notify unity that the Script (MonoBehaviour or ScriptableObject) has been updated.

SetDirty Documentation

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fastest way to instantiate and move multiple game objects in the editor 3 Answers

Unity Editor crashing when editing scripts referenced from GameObjects 1 Answer

Fastest way to instantiate thousands of objects at runtime? 1 Answer

Unity not recognizing Instantiate? JavaScript 1 Answer

The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 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