About 'Import & Refactor Code'!

In this video (objectives)…

  1. Import our NumberWizard.cs code from previous section of the course.
  2. Assess what needs to be refactored / changed in order to have our code work with our user interface.
  3. Refactor the code including player input mechanism.

After watching (learning outcomes)… Refactor our code so that it is ready to hook up with our user interface.

(Unique Video Reference: 8_UI_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

The code in the video doesn’t look quite the same as that I ended up with after the earlier console version. For example, I had no “NextGuess” method. I’m not sure if this is me or a (minor) bug in the lecture…

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NumberWizard : MonoBehaviour
{

    [SerializeField] int max;
    [SerializeField] int min;
    int guess;

    // Use this for initialization
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        max = max + 1;
        guess = (max + min) / 2;
    }

    public void OnPressHigher()
    {
        min = guess;
        NextGuess();
    }

    public void OnPressLower()
    {
        max = guess;
        NextGuess();
    }
    void NextGuess() //This is the missing method he accidentally deletes in lecture 40//
    {
        guess = (max + min) / 2;
    }
}

I have a question about the attaching of scripts to game objects. I noticed we have 2 classes, NumberWizard and SceneLoader, and we create a game object to attach each of these to. What is the reason we don’t attach both to the same game object?

Thanks in advance!

Usually we attach scripts to different Game Objects so that we can stay organised and know where to find things. Sometimes we might want to destroy a Game Object and dont want to accidentally remove additional classes. Also, some Game Objects like SceneLoader might be in every scene, but others may not (eg. if we have a menu screen, we may not want the gameplay functionality to be on the same Game Object).
Hope this helps.
Rick

Privacy & Terms