Search Unity

Script not running

Discussion in 'Getting Started' started by MarkSteere, Mar 1, 2023.

  1. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    This is my first Unity program. I'm starting with a Hello world, and it doesn't seem to be working.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameManager : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {
    Debug.Log("Hi there");

    Setup_Board();
    }

    // Update is called once per frame
    void Update()
    {

    }

    void Setup_Board ()
    {
    GameObject Red_checker_0 = GameObject.Find("Red-checker-container_0");

    Red_checker_0.transform.position = new Vector3(10.0f, 10.0f, 10.0f);
    }
    }

    Do scripts just run automatically, or they have to be initiated with a mouse click on an object or something?
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    You have to attach a script to a game object if you want it to run.

    I would recommend running through the Unity learn lessons as they should cover off all you need to know about this sort of thing.
     
  3. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Ok, that's what I suspected. Thanks for getting back to me.

    I did go through all the Unity tutorials, and then watched a bunch of youtube tutorials on how to make a unity board game, but I don't remember seeing any way of running something like what I was trying to do here... probably because it isn't possible.
     
  4. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    "Hello World" doesn't work in Unity, it's very basic code that works in the majority of languages. Also, for future reference, please use CODE tags when displaying code.
     
  5. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    A "hello world" example is absolutely possible. They key thing here (I recommend) is that you don't actually try to do too much until you understand the basic workings of code in Unity and C# in particular. Learning the "lingo" will help a lot when communicating with others so what you have there is a "class" not what we tend to refer to as a program. Something has to instantiate the class (Unity does that if you've attached it to a game object).

    What you have tried to do is absolutely possible but take small steps to start. Include lots of Debug.Log statements so you can see in the console that the methods were called and in what order. I would add an Awake method to your example (with a Debug.Log statement) and you will be able to see that it is called automatically much like Start.

    If you add Debug.Log statements to the Update you will find pretty quickly that isn't going to work out too well because Update is called many times per second.

    If you are going to change a property, color is a good one to use. Position can be tricky if things end up off the screen for instance.
     
  6. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Ok great, thanks for the information. Attaching a script to an empty game object did the trick in this case, but I still have a lot to learn. Thanks again.
     
  7. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Will do, thanks.