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 Creamzsoda · May 21, 2011 at 07:42 PM · editor-scriptingreset

Editor Script Update Button?

So I'm trying to create an editor script that I can have on a lot of cubes to get them to create fixed joints from a raycast collision. That part works fine.

What I need now is to get it to be able to reapply the whole script with a button push or something. Right now I have to //comment out the @script ExecuteInEditMode(), save, try to reset the script on one of the cubes, go back, uncomment / save / reset the script on one of the cubes and it usually applies to all of them. Sometimes it only applies to that one cube. Any ideas?

Also at the bottom you'll notice a script to remove the joints. Ive been trying to setup this GetComponents loop with a destroy command with no success. : / The current code destroys one joint at a time.


 @script ExecuteInEditMode()
 
 var done : boolean = false;
 var rayRange : float = 4;
 var jointBreakForce : int = 20;
 var jointBreakTorque : int = 10;
 
 function Update () {
     var hit : RaycastHit;
     var distanceToGround = hit.distance;
     var checkJoint;
 
 if (!done){
 
     //Up
       if (Physics.Raycast (transform.position, Vector3.up, hit, rayRange)) {
         Debug.DrawLine (transform.position, hit.point);
         checkJoint = hit.collider.gameObject.GetComponent(FixedJoint);
             if (!checkJoint) {
                 var jointUp =    gameObject.AddComponent(FixedJoint);
                 jointUp.connectedBody = hit.rigidbody;
                 jointUp.breakForce = jointBreakForce;
                 jointUp.breakTorque = jointBreakTorque;
         }
   }
 done = true;
 }
 }
 
 ---------------------------------------
 
 /*
 @script ExecuteInEditMode()
 var removeJoint : boolean = false;
 function Update () {
 if (!removeJoint){
 DestroyImmediate(transform.gameObject.GetComponent(FixedJoint));
 }
 }
 */


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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 21, 2011 at 08:56 PM

That is not an editor script!

ExecuteInEditMore is a little helper to get the same behaviour in the editor as ingame. If you use stuff from the UnityEditor namespace in there you can't even build your game.

I would suggest you write a real editor script and execute your desired actions either with a button inside an editor window or use a menu command.

I would attach a simple script to the cubes/objects on which you want to perform this task. (Maybe rayRange should be a constant in the editor script... would make more sense...)

 // CubeScript.js
 var rayRange : float = 4;
 var jointBreakForce : int = 20;
 var jointBreakTorque : int = 10;

This CubeScript is a normal script so it can be attached to a GameObject.

The following script is an editor script that should be placed in a subfolder that is called "editor". The content of editor folders is not included in your build. It's just available in the editor. If you place this script in your editor folder you will have a new submenu in Unity called "Cube Tools". In this submenu you will find a menuitem "Create Cube Joints". When you click this item you will execute CreateJoints().

This script searches for all CubeScripts in the scene and performs your raycast thing. I've altered the check so one cube can be connected to multiple objects. I just check if the current object is already jointed to another object but it doesn't detect when another object is jointed to the current.

 class CreateCubeJoint extends Editor
 {
     @MenuItem("Cube Tools/Create Cube Joints")
     static function CreateJoints()
     {
         var cubes : CubeScript[] = GameObject.FindObjectsOfType(CubeScript);
         for (var cube in cubes)
         {
             var joints = cube.GetComponents.<FixedJoint>();
             var hit : RaycastHit;
             if (Physics.Raycast (cube.transform.position, Vector3.up, hit, cube.rayRange))
             {
                 var isJointed = false;
                 for (var joint in joints)
                     if (joint.connectedBody == hit.rigidbody)
                         isJointed = true;
                 if (isJointed == false)
                 {
                     var newJoint = cube.gameObject.AddComponent.<FixedJoint>();
                     newJoint.connectedBody = hit.rigidbody;
                     newJoint.breakForce = cube.jointBreakForce;
                     newJoint.breakTorque = cube.jointBreakTorque;
                 }
             }
         }
     }
 }
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 Creamzsoda · May 21, 2011 at 09:19 PM 0
Share

Wow thank you so much! :D I couldn't have asked for a more helpful/thorough response! I really appreciate it.

Going to go implement this right now :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Custom Inspector resets after Unity restarts 0 Answers

Reset script variable values automatically? 2 Answers

Toggle bool reset after using DestroyImmediate (On Editor Script) 0 Answers

How to reset component properties from script 2 Answers

How to prevent Static List from resetting when i run the game in the editor? 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