Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hoeloe · Jun 29, 2013 at 02:00 PM · editorraycasttransformonscenegui

OnSceneGUI has odd behaviour with multiple objects selected

So, I've made a prefab object, and I've written a script to make it stick to my ground as I move it in the editor (I want it to be something that only ever appears flat to the ground, so being able to place it in the editor is nice). My code is here:

     [CustomEditor(typeof(Pad))]
     [CanEditMultipleObjects]
     public class PadEditor : Editor
     {
         void OnSceneGUI()
         {
             RaycastHit h;
             foreach (Transform transform in Selection.transforms)
             {
                 if (transform.gameObject.GetComponent<Pad>() == null) continue;
                 if (Physics.Raycast(transform.position, -transform.up, out h))
                 {
                     if (h.collider.tag == "Ground")
                     {
                         transform.rotation = Quaternion.LookRotation(Vector3.Exclude(h.normal, transform.forward), h.normal);
                         transform.position = h.point + transform.up * 0.05f;
                     }
                 }
                 else
                     if (Physics.Raycast(transform.position, transform.up, out h))
                     {
                         if (h.collider.tag == "Ground")
                         {
                             transform.rotation = Quaternion.LookRotation(Vector3.Exclude(h.normal, transform.forward), h.normal);
                             transform.position = h.point + transform.up * 0.05f;
                         }
                     }
             }
         }
     }

This works absolutely perfectly... as long as I only try and move one object at once. If I select more than one, it works for a little bit, but then starts moving all over the place.

The error seen here occurs by moving the objects along the z-axis only. Once they leave the red object, one object moves correctly, and the other seems to hop about at random.

This occurs just by moving on the Z axis

I'm completely stumped by this. I can't think of anything that would cause this, if anyone else can, then I'd be very grateful if you would give me a hand in working this out. Thanks.

editorsceneguierror.png (415.5 kB)
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

Answer by Sjiggle · Jul 04, 2013 at 11:10 PM

Store all the data you need for the process inside the actual gameobject, rather than storing it on an editor class level basis. It seems that the OnSceneGUI routine gets called for each object, but gets called on the same editor class.

What this basically means is that if you set a variable in the editor class (isDragging = false), the second object has isDragging false. If you set it per target (target.isDragging = false) it will do the same routine in the second target, since you set the status per object, not per editor class.

You use selection.transforms, I used target as my "object". This worked fine in my scenario.

Comment
Add comment · Show 3 · 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 Hoeloe · Jul 04, 2013 at 11:25 PM 0
Share

First of all, as you can see, I'm storing no data for the editor. Secondly, even if I were, I would avoid putting it on the target class if at all possible because I want the efficiency of this code never being looked at in-game. Thirdly, I'm not entirely sure what you mean by using "target", because it is not designed to be used in this way, and throws numerous errors.

avatar image Sjiggle · Jul 04, 2013 at 11:31 PM 0
Share

What I mean by using the target ins$$anonymous$$d of selection.transforms, is the following code I use:

     void OnSceneGUI()
     {
         var csgTarget = target as BrushComponent;
 .......

$$anonymous$$ight be worth a shot, since what you are using right now does not work.

You could avoid cluttering your component classes with irrelevant data by using

 #if UNITY_EDITOR

although you are right, you are not storing any essential data.

avatar image Hoeloe · Jul 04, 2013 at 11:45 PM 0
Share

I tried that, and it seems to work a little better, but I still get the same error, just not as frequently or as severely. I think I'll just set it to not apply to multiple targets for 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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Triangles Index always zero - Custom Editor 1 Answer

Scripting a Click event in the Unity Editor 1 Answer

Working with the editor Selection 0 Answers

Can't move GameObject in Scene view with Unity Components attached 2021.2.2f 0 Answers

Finding a RaycastHit tag (Null Reference Exception) 2 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