Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zak666 · Jul 15, 2016 at 07:38 AM · getcomponentscriptingbasics

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 ...]
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges