Multiplayer typing game issue

So I am making a multiplayer typing game. I have a script running in an empty object which generates the word to be typed.

void Start()
{

    words[0] = "moose";
    words[1] = "cow";
    words[2] = "pig";
    words[3] = "shark";
    words[4] = "dog";


    number = Random.Range(0, 5);
    typing.text = words[number];
    answer = words[number];
    input.Select();
}

void Update()
{
    if (input.text == answer)
    {
        input.text = "";
        number = Random.Range(0, 4);
        typing.text = words[number];
        answer = words[number];
        input.Select();

    }
}

Then I have a script running in the car which tells it to accelerate when you type a word correctly

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;

public class CarBehaviour : NetworkBehaviour {
    private float speed;
    public bool accelerate = false;
    private float zaxis = 1;
    private float xaxis = 0;
    public Text typing;
    public InputField input;


    void Update()
    {
        if (accelerate == true)
        {
            speed = 4;
            accelerate = false;
        }
        else
        {
            speed = 1;
        }
        Vector3 movement = new Vector3(xaxis, 0.00f, zaxis);
        transform.Translate(movement * speed);
     

    }

   void LateUpdate()
    {
        if (input.text == answer)
        {
            accelerate = true;
        }
    }
}

But for some reason whenever I type the word correctly the car won’t accelerate. Any help is appreciated

Thanks.

If the user types the correct word, the input text is set empty. The LateUpdate is never going to see it. You need to have the LateUpdate code in a method called “Accelerate” (or whatever you choose) and then call accelerate from the first script. If you don’t know how to do that, I suggest finding basic C# tutorials on YouTube to get a better understanding of how the code works.

Also, as someone else said the first scripts random generator needs to be 4, not 5. It actually really should be “words.Length - 1” so it always is the amount of words that could be typed.

EDIT: Actually, 5 did work and it was the update using ‘4’ that was wrong. So set it simply to “words.Length”

First off all be aware that in a very rare case your Random.Range(0,5) can return 5f, which would exceed the array of your words.

Is it correct that the car only should move in the zaxis direction? (if your developing a 2D game you wont be able to see movement in z axis if you use a orthographic camera).

xaxis is also set to zero and never changed within the shown code.