enabling and disabling gui elements with time


I added the following script to canvas so i can increase time and when time is right components will be active or disabled. I get following errors.

Assets/Scripts/Manager.cs(16,36): error CS1525: Unexpected symbol <', expecting identifier
Assets/Scripts/Manager.cs(24,27): error CS1525: Unexpected symbol `;’
Assets/Scripts/Manager.cs(26,49): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

This is the code thanks in advance:

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

public class Manager : MonoBehaviour {

	public float time;

	public GUIText text1;
	public GUIText text2;
	public GUIText text3;
	public Image myImageComponent;
	// Use this for initialization
	void Start () {
	// Update is called once per frame
	void Update () {
		if (time<5){


		if (time>5&&time<10){

		if (time>10&&time<15){



Hello, @mkobaner.

There are many minor mistakes in the script. But Problem 3 & 4 will need your attention as it depends on how you plan on implementing this script.

Problem 1

Let’s take a look at this: = GetComponent.< Text >(); and = GetComponent.< Image >();.

One of the problems is the dot after GetComponent. It is causing a syntax error because there should not be a dot in the call of function GetComponent.

Problem 2

The next error is located in the Start function. When calling GetComponent to initialize variables text1, text2 and text3, its parameter is set to Text. That is invalid because those variables’ type is GUIText, not Text.

This is the correct way to initialize those varaibles: = GetComponent< GUIText >(); // No dot after "GetComponent" and replaced Text with GUIText..

Problem 3

There is another problem with the initialization of those variables. The game object can have ONLY ONE GUIText component so the variables are initialized to reference the same GUIText component.

There is no definite solution without full knowledge of how the script will be used and for what exact purpose. But if there isn’t really much to it, then a possible solution is to create two or three empty game objects that contains the GUIText component and set these variables in the inspector instead of the Start function.

Problem 4

Another problem is how variable time is being set in the Updated function. time = time +; The syntax is wrong, it ended with a single plus sign. It is most likely incomplete. I am not sure how it is intended to be changed, so I changed it to: time ++; // How to change [time]?.

Problem 5

The next problem is how isActiveAndEnable is being used. It is used like a method, when it is actually a read-only variable. Meaning, it is only used to check if an object is enabled/disabled. It is not for enabling or disable objects. For further information about it, here is the link to its documentation: Behaviour.isActiveAndEnabled.

To enable/disable a GUIText object, just set its inherited member variable enabled. Example: text1.enabled = true; // true = enabled and false = disabled.. Here is the link to its documentation: Behaviour.enabled.

Here is the fixed version of the script:

To understand the changes and how this script works, please read the entire answer. If there are any questions, please let me know.

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

public class Manager : MonoBehaviour
	// Declaration:
	public		float           time;
	public		GUIText         text1;
	public		GUIText         text2;
	public		GUIText         text3;
	public		Image           myImageComponent;
	// Initialization:
	void Start() {
        // These variables are to be set in the inspector:
		// text1 = GetComponent< Text >(); // Removed dot. Set right param.
		// text2 = GetComponent< Text >(); // Removed dot. Set right param.
		// text3 = GetComponent< Text >(); // Removed dot. Set right param.
		myImageComponent = GetComponent< Image >(); // Removed dot.
	// Called once every frame.
	void Update()
		// How to change [time]?
		time ++;                                    // Removed time = time +;
		if( time < 5 )
		{ text1.enabled = true; }                   // Removed .isActiveAndEnabled.
		else if( time > 5 && time < 10 ) {
			text1.enabled = false;                  // Removed .isActiveAndEnabled.
			text2.enabled = true;                   // Removed .isActiveAndEnabled.
		else if( time > 10 && time < 15 ) {
			text2.enabled = false;                  // Removed .isActiveAndEnabled.
			text3.enabled = true;                   // Removed .isActiveAndEnabled.
			myImageComponent.enabled = false;       // Removed .isActiveAndEnabled.