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;
}
}
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.