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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Tutorial Outdated?

Discussion in 'Scripting' started by Important Business, Apr 30, 2015.

  1. Important Business

    Important Business

    Joined:
    Apr 4, 2015
    Posts:
    16
    The first tutorial's script (for scripting) doesn't work.

    What version should I be using?
     
  2. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    Which tutorial is this?

    Is it the one introducing you to what scripts are with the example of changing a game object's colour?
     
  3. Important Business

    Important Business

    Joined:
    Apr 4, 2015
    Posts:
    16
  4. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Perhaps try something like this for 5.xx

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChangeColor : MonoBehaviour
    5. {
    6.     private Renderer m_Renderer;
    7.  
    8.     void Start()
    9.     {
    10.         m_Renderer = gameObject.GetComponent<Renderer>();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if(Input.GetKeyDown(KeyCode.R)) {
    16.             m_Renderer.material.color = Color.red;
    17.         }
    18.         if(Input.GetKeyDown(KeyCode.G)) {
    19.             m_Renderer.material.color = Color.green;
    20.         }
    21.         if(Input.GetKeyDown(KeyCode.B)) {
    22.             m_Renderer.material.color = Color.blue;
    23.         }
    24.     }
    25. }
     
    CodyG likes this.
  5. Important Business

    Important Business

    Joined:
    Apr 4, 2015
    Posts:
    16
    Still doesnt work, but why not? It really has no reasons when I try to debug it
     
  6. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    How does this differ from yours ??

    added:
    Tried to add a zip file of my project .. but failed ..
     
  7. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    I'm not entirely sure, after trying it in 4.6 and 5 I can't make it not work. Is the script definitely attached to the game object?
     
  8. Important Business

    Important Business

    Joined:
    Apr 4, 2015
    Posts:
    16
    Yup, as a component
     
  9. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    I'm unsure as to what's happening without seeing it. Could you share your code, or even the project you're doing it in?
     
  10. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Ensure that your class name is the same as the Script name .. exactly the same.
     
  11. Important Business

    Important Business

    Joined:
    Apr 4, 2015
    Posts:
    16
    Ive checked the class names and Script name, both exactly the same.
     
  12. Important Business

    Important Business

    Joined:
    Apr 4, 2015
    Posts:
    16
    Eyoo! Just did some checkups, found that the script wasn't attached because I made another script on top of it (something like that). It works now, thanks for all the help!
     
  13. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    Good to hear, good luck in your learning c: