Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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, 2021 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, 2021 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, 2021 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, 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;
         

     }
        
 }

}

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.

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

trouble changing variables of another script 1 Answer

How can keep data when reload the same scene? 0 Answers

please help me, i'm creating a first person game, but my script is incomplete and i can't crouch 0 Answers

Getting wrong value of variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges