- Home /
trying to use get component with find game object: error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
So i have amy game objects here: public GameObject GameGUI; public GameObject redHP; public GameObject blueHP; public GameObject GUIElement; public GameObject Specialobj; public GameObject HPtext; public GameObject SPtext; public GameObject Ammotext; public GameObject Cliptext; public GameObject DownTime;
then in the start method i find and assign the game objects in the scene (since this is a network game and I'm spawning players I have to use findgameobject and cannot directly asian into the inspector also this will make it easier later to make new characters/players)
/// SET GUI:
Ammotext = GameObject.FindGameObjectWithTag ("GUIElement1"); //
Cliptext = GameObject.FindGameObjectWithTag ("GUIElement2"); //
SPtext = GameObject.FindGameObjectWithTag ("GUIElement3");
HPtext = GameObject.FindGameObjectWithTag ("GUIElement4");
Specialobj = GameObject.FindGameObjectWithTag ("GUIElement5");
GUIElement = GameObject.FindGameObjectWithTag ("GUIElement6");
blueHP = GameObject.FindGameObjectWithTag ("GUIElement7");
GameGUI = GameObject.FindGameObjectWithTag ("GameGUI");
DownTime = GameObject.FindGameObjectWithTag ("DownTime");
GameGUI.SetActive (true);
DownTime.SetActive (false);
last in update i am now trying to assign the game objects to getcompoonent to assign the variables to the GUI element here i run into problems.
error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer for all these lines (no idea why when they are defined as a game object in START)
blueHP.GetComponent<Image>().fillAmount = currentHealth; // 100 == fill;
Specialobj.GetComponent<Image>().fillAmount = Special; // 100 == fill;
Ammotext.GetComponent<Text> = Ammo.ToString ();
Cliptext.GetComponent<Text> = clips.ToString ();
HPtext.GetComponent<Text> = currentHealth.ToString ();
SPtext.GetComponent<Text> = Ammotext.ToString ();
//still working on this :D full script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PlayerControll : NetworkBehaviour {
private float turnspeed = 23;
private float FireRate = 0.03f;
public float thrustSpeed = 12;
public float BoosterSpeed = 16;
public static float Special = 0;
public static float Ammo;
public static float Maxammo = 650;
public static float clips = 3;
[SyncVar(hook = "OnChangeHealth")] public int currentHealth = maxHealth;
public const int maxHealth = 100;
public GameObject MachGuns;
public GameObject Explosion;
public Transform GunsSpawn;
public AudioClip GunSound;
private GameObject[] Spawnpoints;
public GameObject Spawnpoint;
public ParticleSystem Thruster;
public ParticleSystem Thruster2;
public GameObject GameGUI;
public GameObject redHP;
public GameObject blueHP;
public GameObject GUIElement;
public GameObject Specialobj;
public GameObject HPtext;
public GameObject SPtext;
public GameObject Ammotext;
public GameObject Cliptext;
public GameObject DownTime;
int index;
//-------------------------------------------------------------------------------------------
// Use this for initialization
void Start () {
// make sure their are carriers.
Spawnpoints = GameObject.FindGameObjectsWithTag("EarthAI2");
index = Random.Range (0, Spawnpoints.Length);
Spawnpoint = Spawnpoints [index];
Spawnpoint = Spawnpoints [index];
this.transform.position = Spawnpoint.transform.position;
//PUT PLAYER AT CARRIER WHEN SPAWN.
if (Ammo == 0) {
Ammo = Maxammo;
}
/// SET GUI:
Ammotext = GameObject.FindGameObjectWithTag ("GUIElement1"); //
Cliptext = GameObject.FindGameObjectWithTag ("GUIElement2"); //
SPtext = GameObject.FindGameObjectWithTag ("GUIElement3");
HPtext = GameObject.FindGameObjectWithTag ("GUIElement4");
Specialobj = GameObject.FindGameObjectWithTag ("GUIElement5");
GUIElement = GameObject.FindGameObjectWithTag ("GUIElement6");
blueHP = GameObject.FindGameObjectWithTag ("GUIElement7");
GameGUI = GameObject.FindGameObjectWithTag ("GameGUI");
DownTime = GameObject.FindGameObjectWithTag ("DownTime");
GameGUI.SetActive (true);
DownTime.SetActive (false);
}
//-------------------------------------------------------------------------------------------
// Update is called once per frame
void Update () {
blueHP.GetComponent<Image>().fillAmount = currentHealth; // 100 == fill;
Specialobj.GetComponent<Image>().fillAmount = Special; // 100 == fill;
Ammotext.GetComponent<Text> = Ammo.ToString ();
Cliptext.GetComponent<Text> = clips.ToString ();
HPtext.GetComponent<Text> = currentHealth.ToString ();
SPtext.GetComponent<Text> = Ammotext.ToString ();
if (Ammo < 1) {
clips -= 1;
Ammo += Maxammo;
}
///GUI CONTROLL
//-------------------------------------------------------------------------------------------
// INPUT GET AXIS CONTROLL
float horizontal = Input.GetAxis ("Horizontal") * turnspeed;
float vertical = Input.GetAxis ("Vertical") * turnspeed;
horizontal *= Time.deltaTime;
vertical *= Time.deltaTime;
transform.Rotate (Vector3.up, horizontal);
transform.Rotate (Vector3.right, vertical);
if (Input.GetButtonDown ("Fire1")) {
if (FireRate < 0.01f && Ammo >0) {
CmdFire ();
FireRate = 0.03f; // resetfirerate
Ammo -= 1;
}
}
if (Input.GetButton ("Boost")) {
transform.Translate (Vector3.forward * BoosterSpeed * Time.deltaTime);
Thruster.startLifetime = 4f;
Thruster2.startLifetime = 4f;
}
if (Input.GetButtonUp ("Boost")) {
transform.Translate (Vector3.forward * BoosterSpeed * Time.deltaTime);
Thruster.startLifetime = 1f;
Thruster2.startLifetime = 1f;
}
//firate controll:
FireRate -= 0.01f;
}
// [ ... snip ...]
}
Answer by Arkaid · Jul 15, 2016 at 07:43 AM
You're missing parenthesis and not assigning a variable.
Ammotext.GetComponent<Text> = Ammo.ToString (); // ERROR
Ammotext.GetComponent<Text>().text = Ammo.ToString (); // OK
I bet it took you more effort to write that wall of text for the question, than it took me to check for syntax errors X3
Srsly, spend 5 minutes debugging your code before posting a question. :3
Your answer
Follow this Question
Related Questions
What's the difference between the two GetComponents? 1 Answer
Accessing variables in another script using GetComponent() 0 Answers
best method for high volume script management 1 Answer
Trigger animation 2 Answers
What is the order of execution for the instantiated object's GetComponent.Script.DoSomething? 1 Answer