Cant change variable speed in script
Hello, i am new in unity and i want to try to change speed from one script to another. Here first script. In this script i want to change speed and for this i using second script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class MoveThinRoller : MonoBehaviour {
public float distance = 1f;
public bool instancedMaterial;
private Rigidbody rb;
private Collider col;
private MeshRenderer mr;
public float speed = 0.5f;
// Use this for initialization
void Start () {
RefreshReferences ();
ChangeSpeed (speed);
}
public void RefreshReferences(){
rb = gameObject.GetComponent<Rigidbody> ();
rb.isKinematic = true;
rb.useGravity = false;
col = gameObject.GetComponent<Collider> ();
if (col == null) {
col = gameObject.AddComponent<MeshCollider> ();
}
mr = gameObject.GetComponent<MeshRenderer> ();
if (mr == null)
mr = gameObject.GetComponentInChildren<MeshRenderer> ();
if (mr == null)
Debug.LogError ("Linear Conveyor needs to be attached to the belt Object");
}
// Update is called once per frame
void FixedUpdate () {
// 'Teleport' rigidbody back and Move forward with physics the same amount each frame
Vector3 mov = transform.forward * Time.deltaTime * speed / distance;
rb.position = (rb.position - mov);
rb.MovePosition (rb.position + mov);
}
public void ChangeSpeed (float _speed) {
// change the speed of the physics and update the shader
speed = _speed;
// Create a new material instance
if (instancedMaterial) {
Material tempMat = new Material (mr.sharedMaterial);
tempMat.SetFloat ("_Speed", speed);
mr.material = tempMat;
} else {
mr.sharedMaterial.SetFloat ("_Speed", speed);
}
}
}
Here is second script, what i use for change speed in first script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpeedOff : MonoBehaviour {
void Start()
{
}
void Update()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Box"){
GameObject varGameObject = GameObject.FindGameObjectWithTag("ChangeSpeed");
MoveThinRoller moveThinRoller = varGameObject.GetComponent<MoveThinRoller>();
moveThinRoller.speed = 0f;
}
}
}
So its doesnt work. Can somebody help me? Thanks. p.s : Sorry for my bad english
What does not work with your current script? Do you have any error?
Dont have any error. Just my code dont change speed to 0f how i want and i dont know why , dont know where is problem.
Answer by CristianLogtech · Jan 22, 2021 at 11:37 AM
So i find little solution, its stop script , i just stop first script from second and after star it again with another its look like stop/start sensors from different sides, but still dont know how to slow down speed. Have somebody some ideas? THX
public class SpeedOff : MonoBehaviour {
public GameObject Manager;
void Start()
{
}
void Update()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Box"){
GameObject varGameObject = GameObject.FindGameObjectWithTag("ChangeSpeed");
Manager.GetComponent<MoveThinRoller>().enabled = false;
}
}
}