Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Regalith · Jul 24, 2012 at 06:53 PM · arraygetcomponent

Incrementing a variable from a script in an Array

Hey im currently working on a 3d version of a board game, and currently trying to increment a variable thats in a script attached to a game object which is inside an array. Heres a chunk of the script so far.

 function OnGUI(){
 
  
 
  if(musterMenu == true && fortress > 0)
  {
  GUI.Box (Rect (Screen.width/2 - 440,Screen.height/2 - 60 ,900, 200), "Mustering");
  GUI.Label(Rect(Screen.width/2 - 355,Screen.height/2 - 35,64,64),footmanTexture);
 
  if(GUI.Button(Rect(Screen.width/2 - 420,Screen.height/2 +35, 200, 30),"Footman"))
  {
  Instantiate(1);
  }
  if(GUI.Button(Rect(Screen.width/2 - 210,Screen.height/2 +35, 200, 30),"Horseman"))
  {
  Instantiate(2);
  }
  if(GUI.Button(Rect(Screen.width/2 + 10 ,Screen.height/2 +35, 200, 30),"Ram"))
  {
  Instantiate(3);
  }
  if(seas.length > 0){
  if(GUI.Button(Rect(Screen.width/2 + 220,Screen.height/2 +35, 200, 30),"Boat"))
  {
  boatPlacement = true;
  }
  }
  if(GUI.Button(Rect(Screen.width/2 - 210,Screen.height/2 + 70, 200, 30),"Upgrade to Horseman"))
  {
  Instantiate(5);
  }
  if(GUI.Button(Rect(Screen.width/2 + 10,Screen.height/2 + 70, 200, 30),"Upgrade to Ram"))
  {
  Instantiate(6);
  }
  if(GUI.Button(Rect(Screen.width/2 + 340,Screen.height/2 + 90, 100, 40),"Return"))
  {
  musterMenu = false;
  }
  }
  
  //GUI for Boat placement
  
  if(boatPlacement == true)
  {
  musterMenu = false;
  GUI.Box (Rect (Screen.width/2 - 340,Screen.height/2 - 60 ,700, 150), "Place Boat At");
  
  if(seas.length == 1)
  {
  if(GUI.Button(Rect(Screen.width/2 - 320,Screen.height/2 - 10, 200, 40),seas[0].tag))
  {
  location = seas[0].transform.position;
  seas[2].GetComponent("Territory").unitCount++;
  UnitPlacement(4);
  boatPlacement = false;
  }
  }
  if(seas.length == 2)
  {
  if(GUI.Button(Rect(Screen.width/2 - 320,Screen.height/2 - 10, 200, 40),seas[0].tag))
  {
  location = seas[0].transform.position;
  UnitPlacement(4);
  boatPlacement = false;
  }
  
  if(GUI.Button(Rect(Screen.width/2 - 100,Screen.height/2 - 10, 200, 40),seas[1].tag))
  {
  location = seas[1].transform.position;
  UnitPlacement(4);
  boatPlacement = false;
  }
  }
  if(seas.length == 3)
  {
  if(GUI.Button(Rect(Screen.width/2 - 320,Screen.height/2 - 10, 200, 40),seas[0].tag))
  {
  location = seas[0].transform.position;
  UnitPlacement(4);
  boatPlacement = false;
  }
  
  if(GUI.Button(Rect(Screen.width/2 - 100,Screen.height/2 - 10, 200, 40),seas[1].tag))
  {
  location = seas[1].transform.position;
  UnitPlacement(4);
  boatPlacement = false;
  }
  if(GUI.Button(Rect(Screen.width/2 + 120,Screen.height/2 - 10, 200, 40),seas[2].tag))
  {
  location = seas[2].transform.position;
  UnitPlacement(4);
  boatPlacement = false;
  }
  }
  if(GUI.Button(Rect(Screen.width/2 -100,Screen.height/2 + 40, 100, 40),"Return"))
  {
  boatPlacement = false;
  }
  
  }
 }

the variable im trying to access is in OnGUI() function, and is called unitCount. the script on the game objects are identical to this script, and the only thing i want to do is increment unitCount at seas[2].GetComponent("Territory").unitCount++; <-- this doesnt work so how can i get it to increment?

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
0
Best Answer

Answer by Bryson · Jul 25, 2012 at 08:11 AM

As it is now the line

 seas[2].GetComponent("Territory").unitCount++;

is getting a copy of Territory's unitCount variable, without storing it, and incrementing that copy. You could actually set the value using something like

 seas[2].GetComponent("Territory").unitCount = seas[2].GetComponent("Territory").unitCount++;

or you could write a simple function in Territory to increment unitCount and call it from OnGUI().

Comment
Add comment · Show 3 · 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
avatar image Mizuho · Jul 25, 2012 at 09:44 AM 0
Share

Even simpler is just seas[2].GetComponent(&quot;Territory&quot;).unitCount += 1.

avatar image Regalith · Jul 26, 2012 at 06:39 PM 0
Share

thanks the first one didnt work, but the second one did so thanks for the help

avatar image Bryson · Jul 26, 2012 at 06:44 PM 0
Share

What is the type of unitCount - int? Is it a public variable? Also, why seas.get_Item(2) ins$$anonymous$$d of seas[2]? I'm not sure, but maybe this is returning a copy of the item, not a reference to the item itself.

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

6 People are following this question.

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

Related Questions

Find GameObjects with a true boolean and put them in an array? 1 Answer

How to access multiple components of a component at once? 2 Answers

How would I write this? 1 Answer

Using getcomponent with an array 2 Answers

Get variable in script got by get component c# 2 Answers


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