Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script doesn't work fully on other objects

Discussion in 'Getting Started' started by bah2, Jan 22, 2020.

  1. bah2

    bah2

    Joined:
    Jan 9, 2020
    Posts:
    5
    Actually I just solved this by copying the original code and pasting it to a new script, but if anyone knows what actually causes this I would be thankful:

    While prototyping and testing I wrote a simple script for my sphere (with a cube on one side to signify the facing, made in to a prefab and the sript assigned to it) in which W moves it forward and A and D rotate it to left and right, but now if I use the same script for any other object (be it a unity primitive or a blender object or even a copy of the same prefab) it won't rotate anymore. The script has been assigned correctly, since it moves forward just fine. What gives?
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Please share your script here, using the proper tags. Very difficult to help when we cant see the code.
     
  3. bah2

    bah2

    Joined:
    Jan 9, 2020
    Posts:
    5
    Yeah, thought of that at first but since
    a) there were no changes in the previously working code
    b) the code worked just fine when I copy-pasted it 1:1 to another script (excluding the class name, ofc)
    I deemed it unnecessary. If I had changed anything in the code, then yes, this would've been included.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     public float speed = 5f;
    10.     public float turnSpeed=45f;
    11.    
    12.  
    13.     void Start()
    14.     {      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetKey(KeyCode.LeftShift))
    21.             speed = 10;
    22.         else
    23.             speed = 5;      
    24.  
    25.         if (Input.GetKey("w"))
    26.         {          
    27.             transform.Translate(Vector3.forward * speed * Time.deltaTime);          
    28.         }
    29.          
    30.         if (Input.GetKey("a"))
    31.         {
    32.             transform.Rotate(Vector3.up * -turnSpeed * Time.deltaTime);
    33.         }
    34.         if (Input.GetKey("d"))
    35.         {
    36.             transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
    37.         }
    38.     }
    39. }
    40.  
    More relevant information would perhaps have been the version I'm using (which I also forgot to mention) which is 2019.2.17f1