Search Unity

Question Void Update not being called in this particular script

Discussion in 'Scripting' started by CurzedWarrior1066, Sep 28, 2022.

  1. CurzedWarrior1066

    CurzedWarrior1066

    Joined:
    Sep 19, 2022
    Posts:
    2
    I am trying to recreate space invaders for a school project and I have been having difficultly with the void Update() not being called in this particular script. I know that script works if I do the stuff in the void Update in a new script and attach it to the same game object. I just can not figure out why it will not work within the larger script.Thanks
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Invaders : MonoBehaviour
    4. {
    5.     public Invader[] prefabs;       //Array of prefabs called invaders
    6.     public float speed = 5f;
    7.     public int rows = 5;
    8.     public int columns = 11;
    9.  
    10.     private void Awake()
    11.     {
    12.         for (int row = 0; row < this.rows; row++)                                                    //Spacing between invaders vertically
    13.         {
    14.             float width  = 2.0f * (this.columns -1);                                                 //This give me the total width and height of the columns and rows based on the
    15.             float height = 2.0f * (this.rows - 1);                                                   //spacing that is set multiplied by the row or column take away 1
    16.             Vector3 centering = new Vector2(-width / 2, -height / 2);                                //Finding the centre of the grid pattern
    17.             Vector3 rowPosition = new Vector3(centering.x, centering.y + (row * 2.0f), 0.0f);        //setting the row position using the centered x and y values
    18.  
    19.             for (int col = 0; col < this.columns; col++)
    20.             {
    21.                 Invader invader = Instantiate(this.prefabs[row], this.transform);                    //spawning the Invaders from the prefabs within the row. It also stores the result of this to invader
    22.                 Vector3 position = rowPosition;
    23.                 position.x += col * 2.0f;                                                            //Spacing between the invaders horizontally
    24.                 invader.transform.localPosition = position;
    25.             }
    26.         }
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.  
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         transform.Translate(speed * Time.deltaTime, 0, 0);  
    37.     }
    38. }
     
  2. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    1. Check if both the script is enabled and the GameObject it's on is active in the hierarchy. Both are checkboxes you can see in the inspector. Also that the script is added to the correct GameObject.

    2. Put a Debug.Log in it to make sure you are diagnosing it correctly, that the Update is actually not called, and not just that the logic of your code is unexpectedly having no effect.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    Please post general scripting problems on the Scripting forum please. The 2D forum should be used to post about 2D specific things.

    I'll move this post there.

    Thanks.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    So you know, it's just called "Update". void is just a keyword meaning that it doesn't return anything, it's not its name.

    Here's a useful link on how to perform debugging. This'll help you narrow down your problem such as adding in a Debug.Log in the "Update" method as suggested above; something like Debug.Log("It's alive")