Search Unity

Same Script for Multiple GameObjects workflow

Discussion in 'Scripting' started by theovenigma, Jun 27, 2021.

  1. theovenigma

    theovenigma

    Joined:
    Jun 22, 2021
    Posts:
    3
    So, I have created a single script called MovementEngine and attached it to 5 gameobjects. Inside the script I have :

    Code (CSharp):
    1. bool move;
    2.  
    3. void Start(){
    4. }
    5.  
    6. void Update(){
    7.     if(move){
    8.         Debug.Log('Hello it's moving");  // The log never show this line
    9.        move = false;
    10.    }
    11. }
    12.  
    13. public void MoveObject (){
    14.    move = true;
    15.    Debug.Log("Move is true"); // The log shows this
    16. }
    I called the MoveObject function from another script called MainEngine. Inside MainEngine :

    Code (CSharp):
    1. public GameObject objectA;
    2. public GameObject objectB;
    3. public GameObject objectC;
    4.  
    5. void Update{
    6.     if(Input.GetKeyDown(KeyCode.Space){
    7.         objectA.GetComponent<MovementEngine>().MoveObject();
    8.         objectB.GetComponent<MovementEngine>().MoveObject();
    9.     }
    10. }
    Somehow after it logs "Move is true" , the update did not detect the "move" variable as true. Is this caused by the usage of the same script for multiple object?
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Hi,

    Are you sure you are calling the right instance of your script, and it's enabled, and the GameObject containing it also is active in the scene?

    Add the name to the Debug.Log to be sure.
    Debug.Log("Move is: " + move + ", name: " + gameObject.name + ", and is active: " + isActiveAndEnabled);