• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CristianLogtech · Jan 21 at 02:56 PM · unity 5variablescripting beginnerspeedchange

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jan 21 at 01:35 PM 0
Share

What does not work with your current script? Do you have any error?

avatar image CristianLogtech Hellium · Jan 22 at 06:32 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CristianLogtech · Jan 22 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;
         

     }
        
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

explain me the script 0 Answers

C# Unity dot syntax exercise correct solution? 1 Answer

Problem with Animation How to Move Property 0 Answers

Can anyone explain what an offset is? 0 Answers

I have a jump scare and I want it to affect my player's health when it activates it and kill my player when health = 0. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges