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 Grasshorse · Oct 12, 2011 at 01:52 AM · javascriptarraycolorguitexture

Assigning GUITexture through Javascript

Hello,

I've been trying to get a bit of code working. below is a truncated version for demonstration purposes. I'm trying to figure out how to store an array of GUITextures, that I can then access values to. I've banged my head against this all day, and I have not found a very good example online of how this is achieved. Here is what I am trying to do.

I have been able to create the array, but not populate it. Since, I have not achieved that I also do not know if the color assignment, position and scale variables have been set. I think those should work, but then a again, I must be missing something fundamental. Is GUITextures just not intended to be scripted? I find that hard to believe. If anyone has any ideas I would love to hear them.

Best Regards,

Stephen

 var numberOfButtons:int = 7;

 var buttonBGArray = new GUITexture[numberOfButtons];

 for(var x:int = 0; x < numberOfButtons; x++)
 {
   var bgColor:Color;
     bgColor = Color(1,0,0,.5);
     
     buttonBGArray[x].texture = textureArray[categoryTextureNumber];
     buttonBGArray[x].color = bgColor;
     buttonBGArray[x].transform.position = Vector2(xPlacement,yPlacement);
     buttonBGArray[x].transform.localScale = Vector2(buttonWidth,buttonHeight);
 }
Comment
Add comment · Show 3
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 jahroy · Oct 12, 2011 at 02:40 AM 0
Share

What is textureArray? What is categoryTextureNumber?

avatar image Grasshorse · Oct 12, 2011 at 02:52 AM 0
Share

Sure thing. "textureArray" is a Texture2D[] Array that I created where I store all image files that are going to be used in the application. These are loaded from the Resources directory.

I also have a Hashtable that is populated from an X$$anonymous$$L file that is parsed. This Hashtable contains variables and properties including texture information. "categoryTextureNumber" is a value that associates the the textureArray to the Hashtable. $$anonymous$$inda hard to explain actually.

Ultimately I am trying to associate a Texture2D with the GUITexture .texture variable.

avatar image jahroy · Oct 12, 2011 at 04:44 AM 0
Share

That sounds pretty straightforward.

Are you sure that these objects are correctly initialized?

I don't see anything wrong with your code, so I would start with some debug statements.

First I would make sure that textureArray[categoryTextureNumber] returns a valid Texture by printing some simple imformation to the console.

I would also add a "test" for loop right after you're done populating the buttonBGArray to make sure the first for loop worked. Something as simple as this:

 for ( var thisButt : GUITexture in buttonBGArray ) {
     Debug.Log("color: " + thisButt.color);
 }

If that doesn't work then you know your problem is somewhere else.

Sorry, not trying to be insulting.

It's just that your code looks good and you don't show any of your other code, so there's a chance that the error could be somewhere else.

1 Reply

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

Answer by aldonaletto · Oct 12, 2011 at 05:11 AM

The array buttonBGArray only holds the references, not the GUITextures themselves. I suspect that for each element you must create an empty GameObject and add to it a GUITexture with AddComponent:

for(var x:int = 0; x < numberOfButtons; x++) { var gObj = new GameObject("GO"+x); // create the game object gObj.AddComponent("GUITexture"); // add the GUITexture... buttonBGArray[x] = gObj.guiTexture; // and save its reference buttonBGArray[x].texture = ... ...

Comment
Add comment · Show 1 · 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 Grasshorse · Oct 12, 2011 at 03:48 PM 0
Share

That is really cool. It worked like a charm. $$anonymous$$any, many thanks! So it looks like all of the properties like Color, position, etc. are able to be assigned once the GUITexture is referenced to the Array. Pretty powerful stuff. Thank you again for a point in the right direction!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Issue with some GUITextures and an Array [SOLVED] 1 Answer

Help converting this to C# - a few issues 3 Answers

Array in inventory, adding and greater than 1 Answer

Get MeshRenderer Component of Gameobjects in an Array 1 Answer

Reference JS array of Objects = errors 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