0
\$\begingroup\$

I'm trying to create a falling word typing game like z-type.

I have used the code provided here

I want to create multiple levels in the game, and it seems like I need to make some changes in the code for each level.

For example, in the word display file, I want the active word to be changed to red for level 1 and blue for level 2.

So I created a copy of my level 1 scene, created a duplicate WordDisplay script file, changed the code for active word to blue....

public class WordDisplay : MonoBehaviour {
    public Text text;
    public float fallSpeed = 1f;

    public void SetWord (string word)
    {
        text.text = word;
    }

    public void RemoveLetter ()
    {
        text.text = text.text.Remove(0, 1);
        text.color = Color.red;
    }
    //...

Here's the duplicate script I made:

public class WordDisplay1 : MonoBehaviour {
    public Text text;
    public float fallSpeed = 1f;

    public void SetWord (string word)
    {
        text.text = word;
    }

    public void RemoveLetter ()
    {
        text.text = text.text.Remove(0, 1);
        text.color = Color.blue;
    }
    //...

Then I made a copy of the WordSpawner as well because I was getting an error: I had to change the method's return type to to WordDisplay1 and then got one more error which was in the Word file, leading to more changes...

public class WordSpawner : MonoBehaviour {
    public GameObject wordPrefab;
    public Transform wordCanvas;

    public WordDisplay SpawnWord()
    {
        //...

public class WordSpawner1 : MonoBehaviour {
    public GameObject wordPrefab;
    public Transform wordCanvas;

    public WordDisplay1 SpawnWord1()
    {
        //...

Then I created a duplicate of the word prefab and selected the appropriate word spawner file...

All of this seems like too many changes for a simple colour.

Is there a better way to have multiple levels without having to create a duplicate of all these files and the word prefab?

\$\endgroup\$
1
\$\begingroup\$

In programming we have a principle called DRY: Don't Repeat Yourself.

If you ever find yourself copying and pasting a big chunk of code, DRY says "Stop. Is there another way we can share this functionality, without repeating the code?"

In particular, you should never copy a whole script file just to change a value used inside it. That's what variables are for.

So, step 1: add a variable to control the colour at the top of the class, and make it public so we can set it in the inspector.

public class WordDisplay : MonoBehaviour {

    public Text text;
    public float fallSpeed = 1f;
    // New variable! You can even initialize it to a default colour if you want.
    public Color color;

Step 2: use the variable instead of a hard-coded colour.

    public void RemoveLetter ()
    {
        text.text = text.text.Remove(0, 1);
        // No more hard-coded value - we just use whatever the level designer set in the Inspector.
        text.color = color;
    }

Step 3: select the prefab instance in level 1 and set the colour you want there.

Step 4: select the prefab instance in level 2 and set the colour you want there.

You can now have as many different levels and colours as you want, with no further script changes, and no need to duplicate all your files.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.