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

Help with "Attach this script to a GameObject"

Discussion in 'Scripting' started by PickyBiker, May 15, 2020.

  1. PickyBiker

    PickyBiker

    Joined:
    May 11, 2020
    Posts:
    9
    I am new to Unity and trying to rotate an object. I found the example script (below) that indicates I should attach it to a GameObject. I tried to drag and drop the C# script to the object (Cube) but that generates an error, "Can't add script behavior CallbackExecutor. The script needs to derive from MonoBehaviour."

    The object in question is Cube, the C# script is "Rotator" and the class name is "Rotator", so how do you attach the script to a GameObject named Cube?

    Code (CSharp):
    1.  
    2. //Attach this script to a GameObject
    3. //This example shows the difference between using Space.world and Space.self.
    4. //Enable or disable the checkbox in the Inspector before starting (depending on if you want world or self)
    5. //Press play to see the GameObject rotating appropriately. Press space to switch between world and self.
    6.  
    7. using UnityEngine;
    8.  
    9. public class Rotator : MonoBehaviour
    10. {
    11.     float m_Speed;
    12.     bool m_WorldSpace;
    13.  
    14.     void Start()
    15.     {
    16.         //Set the speed of the rotation
    17.         m_Speed = 20.0f;
    18.         //Start off in World.Space
    19.         m_WorldSpace = true;
    20.         //Rotate the GameObject a little at the start to show the difference between Space and Local
    21.         transform.Rotate(60, 0, 60);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         //Rotate the GameObject in World Space if in the m_WorldSpace state
    27.         if (m_WorldSpace)
    28.             transform.Rotate(Vector3.up * m_Speed * Time.deltaTime, Space.World);
    29.         //Otherwise, rotate the GameObject in local space
    30.         else
    31.             transform.Rotate(Vector3.up * m_Speed * Time.deltaTime, Space.Self);
    32.  
    33.         //Press the Space button to switch between world and local space states
    34.         if (Input.GetKeyDown(KeyCode.Space))
    35.         {
    36.             //Make the current state switch to the other state
    37.             m_WorldSpace = !m_WorldSpace;
    38.             //Output the Current state to the console
    39.             Debug.Log("World Space : " + m_WorldSpace.ToString());
    40.         }
    41.     }
    42. }
    43.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Most likely that error is being shown because you have script compilation errors. If you open the console window and hit clear, you should be left with only compiler errors; what are they?
     
    PraetorBlue and Joe-Censored like this.
  3. PickyBiker

    PickyBiker

    Joined:
    May 11, 2020
    Posts:
    9
    You were correct sir. There was a naming error because there was another item already named Rotator. I fixed that and now it is working.

    Thanks for the help.
     
    Kurt-Dekker likes this.