Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to destroy objects to minimum value in Range attribute?

Discussion in 'Scripting' started by Chocolade, Jan 25, 2023.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    The mono script:

    Code (csharp):
    1.  
    2. using BezierSolution;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEditor.TerrainTools;
    6. using UnityEngine;
    7.  
    8. public class RandomBezierSpline : MonoBehaviour
    9. {
    10.     [Header("Bezier Spline")]
    11.     [Range(2f, 100f)]
    12.     public int numberOfBeziers = 2;
    13.     public bool randomNumberOfBeziers = false;
    14.     public BezierSpline bezierSpline;
    15.  
    16.     private int oldNumberOfBeziers;
    17.  
    18.     public void GenerateBezier()
    19.     {
    20.         if (oldNumberOfBeziers != numberOfBeziers)
    21.         {
    22.             bezierSpline.Initialize(numberOfBeziers);
    23.  
    24.             oldNumberOfBeziers = numberOfBeziers;
    25.         }
    26.     }
    27. }
    28.  
    The editor script:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using UnityEditor.TerrainTools;
    6. using UnityEngine;
    7.  
    8. [CustomEditor(typeof(RandomBezierSpline))]
    9. public class RandomBezierSplineEditor :Editor
    10. {
    11.     public override void OnInspectorGUI()
    12.     {
    13.         base.OnInspectorGUI();
    14.  
    15.         RandomBezierSpline randomBezierSpline = (RandomBezierSpline)target;
    16.  
    17.         randomBezierSpline.GenerateBezier();
    18.     }
    19. }
    20.  
    The script the part of the script creating the objects:

    Code (csharp):
    1.  
    2. public void Initialize( int endPointsCount )
    3.         {
    4.             if (endPointsCount <= 2)
    5.             {
    6.                 return;
    7.             }
    8.  
    9.             // Destroy current end points
    10.             endPoints.Clear();
    11.             GetComponentsInChildren( endPoints );
    12.  
    13.             for( int i = endPoints.Count - 1; i >= 0; i-- )
    14.                 DestroyImmediate( endPoints[i].gameObject );
    15.  
    16.             // Create new end points
    17.             endPoints.Clear();
    18.  
    19.             for( int i = 0; i < endPointsCount; i++ )
    20.                 InsertNewPointAt( i );
    21.  
    22.             Refresh();
    23.         }
    24.  
    The problem is when i move the Range slider in the inspector to the right increasing the value it's creating new Point's objects. but then when i slide the Range slider to the left decreasing the value it's destroying the Point's but when the slider is at value 0 there are still Point's left sometimes 2 sometimes 4 sometimes more or less but it's never destroyed all the Point's objects when getting to the value 0.

    Update :

    i tested it again and it seems like when I'm sliding the Range slider too fast to the left side it can't handle it and can't destroy all the Point's objects. the fastest I'm moving the Range slider the more Point's left. if i move the slider very slow the best i can get is left with 3 Point's but it should be 2 the minimum of Point's is 2.

    is there a way to work around this slider moving speed problem?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Your function tests and exits early.

     
    Bunny83 and Chocolade like this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    i didn't explain it good enough.

    the problem is when moving the mouse very fast when dragging the slider when changing the slider value then the slider miss values. if for example the range is 0 to 30 and i move the mouse very fast on the slider from 0 to 20 then it will miss("jump" over) some of the values. so if i will type the values on screen i might see the values 1 5 12,13,14, 24
    instead 1,2,3,4,5,6,7,8,9,10,11...30

    the slider event can't capture the mouse speed to show the values. maybe because the slider have some delay ?
    is there a way to create a custom slider that will show/capture the values no matter how fast i move the mouse ?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    I don't think precision of the slider matters when you're destroying all children and recreating them each time you change a value in the inspector. There's nothing in your code you've shown that depends on
    endPointsCount
    when destroying points.

    Though it'd probably be better to keep direct references to these points rather than using GetComponentsInChildren to find them every time. Just need your own list that you add/remove them from.
     
    Chocolade and Bunny83 like this.