fortune wheel spinner

Hey everyone i have script for a wheel spinner is working fine but i want to add button that’s make me respin the wheel exactly replace button with the KeyCode.Space in code C# below
i’m beginner in unity & coding and thank u

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spinner : MonoBehaviour
{

    public float reducer;
    public float multiplier = 1;
    bool round1 = false;
    public bool isStoped = false;


    void Start()
    {
        reducer = Random.Range(0.01f, 0.5f);
    }

    // Update is called once per frameQ
    void FixedUpdate()
    {

        if (Input.GetKey(KeyCode.Space))
        {
            Reset();
        }

        if (multiplier > 0)
        {
            transform.Rotate(Vector3.forward, 1 * multiplier);
        }
        else
        {
            isStoped = true;
        }

        if (multiplier < 20 && !round1)
        {
            multiplier += 0.1f;
        }
        else
        {
            round1 = true;
        }

        if (round1 && multiplier > 0)
        {
            multiplier -= reducer;
        }
    }


    void Reset()
    {
        multiplier = 1;
        reducer = Random.Range(0.01f, 0.5f);
        round1 = false;
        isStoped = false;
    }
}

no one !!??

Here is a Unity video demonstrating the use of UI buttons.

Here is an API link to check out.

Also, you should change your FixedUpdate (for fixed time step calc’s- typically used for physics) to be Update (once per frame).

1 Like

i try my best still don’t work for me :cry:
i add button and edit the script , but there is no function show up

public float reducer;
    public float multiplier = 1;
    bool round1 = false;
    public bool isStoped = false;
    private Button spinn;

    void Start()
    {
        reducer = Random.Range(0.01f, 0.5f);
    }

    private void Awake()
    {
        spinn = GetComponent<Button>();
    }

    // Update is called once per frameQ
    void Update()
    {

        if (Input.GetKey(KeyCode.Space))
        {
            spinn.onClick.Invoke();
            Reset();
        }

Make Reset() a public method.

public void Reset()
{
}

Once that is done, select the gameobject with your button in the unity’s hierarchy. At the bottom of the “Button” component, on the gameobject, there should be a section that says “OnClick”. Everything on there will be called when the button is clicked.

Press the “+” to add a method to be called. Then drag and drop the gameobject that holds your “spinner” script into the empty field you just created. As you do that, you will be able to edit the field to the right. Select your “spinner” class from the list, and then select your newly public “Reset()” method.

1 Like

Man your are angel its worked for me finally
thank you so much Doug_B & AntoineDesbiens

1 Like