Я пытаюсь использовать ползунок пользовательского интерфейса, чтобы изменить скорость перемещения персонажа игрока с помощью функции GetComponent.У меня все работает, кроме применения числа (float), которое я создаю от движения ползунка к переменной, которая контролирует, как быстро игрок может двигаться.
Я использовал Debug.Log ();чтобы определить, что переменная, которую я пытаюсь получить из одного скрипта, не равна другой.Кажется, что они хранятся в виде двух отдельных переменных.
Переменная varspeed отслеживает число при перемещении ползунка.
В сценарии BallScript:
GameObject.Find("Canvas").GetComponent<PointBuyScript>().varspeed = speedvar1;
Debug.Log(speedvar1);
В скрипте PointBuyScript:
public void Start()
{
mySpeed.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
}
public void LateUpdate()
{
varspeed = mySpeed.value;
Debug.Log(varspeed);
}
При перемещении ползунка число в консоли из шкал PointBuyScript с помощью ползунка.Тем не менее, один из BallScript навсегда остается прежним.
Код BallScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BallScript : MonoBehaviour
{
// Start is called before the first frame update
public float speed;
private Rigidbody rb;
public float speedvar1;
public float SpeedMain;
void Start()
{
rb = GetComponent<Rigidbody>();
speedvar1 = GameObject.Find("Canvas").GetComponent<PointBuyScript>().mySpeed.value;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.00f, moveVertical);
speed = speedvar1; // this is where I try and update the speed variable to the slider number
rb.AddForce(movement * speed);
}
void LateUpdate()
{
Debug.Log(speedvar1);
Debug.Log(speed);
// Debug.Log(SpeedMain);
}
}
Код PointBuyScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PointBuyScript : MonoBehaviour
{
public Slider mySpeed;
public float varspeed;
public float mainSpeed;
public void Start()
{
// GameObject speed1 = GameObject.Find("Ball");
// BallScript hellome = speed1.GetComponent<BallScript>();
// varspeed = GameObject.Find("Ball").GetComponent<BallScript>().speed;
//Adds a listener to the main slider and invokes a method when the value changes.
mySpeed.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
}
public void Update()
{
// Debug.Log(mySpeed.value);
//mainSpeed = mySpeed.value;
}
public void LateUpdate()
{
varspeed = mySpeed.value;
Debug.Log(varspeed);
}
// Invoked when the value of the slider changes.
public void ValueChangeCheck()
{
}
}