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 Simple Slider issue?

Discussion in 'Scripting' started by riasillo, Sep 30, 2023.

  1. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    24
    Type mismatch error when adding slider component in inspector?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ShotPower : MonoBehaviour
    5. {
    6.     public float Force;
    7.     public Slider forceUI;
    Following a simple tutorial and failing.

    Thought maybe update change since tutorial but only 1 year old.
    Tried using elements but that doesn't show field in inspector.

    Created UI Slider in hierarchy, it created a game object with slider component.
    Am I using correct UnityEngine.UI or is there another system to cover this component?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    I think you do want that one, yes...

    Did you make your OWN class called Slider?
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,048
    Do you want a reference in Inspector where you can drag a Slider component onto? Or do you want the Force value to be editable as a slider?

    In the latter case you only need to prepend it with the Range attribute:

    [Range(0f, 1f)] public float Force;
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Make sure there aren’t any other compile errors. You have to fix those first if that is the case.
    Check the console.
    Make sure you’re doing everything the same as in the tutorial. You can’t make any typo’s
     
  5. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    24
    thanks for responses. The issue was trying to attach it to a prefab. In tutorial they were adding to a ball in hierarch. I was trying to add a shot strength meter to a ball prefab. Unity is one fickle beast. No issue dragging and dropping TMPs or other things on prefabs, but the slider must be done with prefab unpacked it would seem. Works now. Thanks.
     
  6. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    24
    No, but unity did flag as ambiguous, and showed solutions, which is how I knew about elements. Thanks, issue was dragging on a prefab that wasn't unpacked.
     
  7. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    24
    Not sure what I really wanted, which is part of the problem ;-) never used slider before, but it did the trick. I wanted to be able to edit shot strength in inspector, click button, have a smooth shot strength meter. Works very good, after tweaking some settings between increments. and min max settings on slider, got it to slide nice.
    This is for a pinball game.
    I ended up with max force 12, increment 8, slider was set to 0 to 10.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ShotPower : MonoBehaviour
    7. {
    8.     public float maxForce = 10.0f; // Maximum force that can be applied
    9.     public float forceIncrement = 1.0f; // Incremental force increase per second
    10.  
    11.     private float currentForce = 0.0f;
    12.     private bool isIncreasingForce = false;
    13.  
    14.     public Slider forceUI;
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.UpArrow))
    20.         {
    21.             isIncreasingForce = true;
    22.         }
    23.  
    24.         if (Input.GetKeyUp(KeyCode.UpArrow))
    25.         {
    26.             isIncreasingForce = false;
    27.             shoot();
    28.             StartCoroutine(Wait());
    29.         }
    30.  
    31.         if (isIncreasingForce)
    32.         {
    33.             IncreaseForce();
    34.             slider();
    35.         }
    36.     }
    37.  
    38.     void IncreaseForce()
    39.     {
    40.         currentForce += forceIncrement * Time.deltaTime;
    41.         currentForce = Mathf.Clamp(currentForce, 0, maxForce);
    42.     }
    43.  
    44.     void shoot()
    45.     {
    46.         // Calculate the direction based on the Z-axis (forward)
    47.         Vector3 shootDirection = transform.forward;
    48.  
    49.         // Apply force in the forward direction
    50.         GetComponent<Rigidbody>().AddForce(shootDirection * currentForce, ForceMode.Impulse);
    51.         currentForce = 0; // Reset the force after shooting
    52.     }
    53.  
    54.     public void slider()
    55.     {
    56.         forceUI.value = currentForce;
    57.     }
    58.  
    59.     IEnumerator Wait()
    60.     {
    61.         yield return new WaitForSeconds(1.5f);
    62.         slider(); // Update the slider after the wait period
    63.     }
    64. }
    65.  
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Pinball... neat!!

    You might want to use an
    AnimationCurve
    , basically an analog lookup table, to control strength of shot based on how far the plunger is withdrawn.