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
1
Question by J-Brinky · Apr 24, 2011 at 08:08 PM · guiinstantiatecs0236

Instantiate gui.Text issue

Hi,

I am facing an issue, which i have been trying to solve for the past few hours, but no luck. My problem:

I have an interface with 5 buttons, each with their unique "id" set, which retrieves information from an data file and shows the data on the Gui.Text prefab i have setup.

Prefab: a guitext component named itemText

What it doesn:

It creates a set of buttons made from the arrayList and shows them correctly on the GUI - clicking one of those buttons, retrieves the information and displays it correctly. Although when i click another button in the same session, it seems it has duplicated the info from the first button, even though the data and the button "id" are unique!

What i'd like it to do is that when you click on another button, the "previous" object is overwritten, is there any way todo this ?

I tried destroying the prefab before initializing / on startup and when there is already some "text" retrieved, no luck on that :(

This is the code i have (some methods are removed)

public class ItemSelection : MonoBehaviour {

float ScreenWidth; float ScreenHeight;

public string item_name; public GameObject itemText;

IEnumerator getItems(int id){ // www classs data = update_get.text; GetLevel(dataArray); ShowInfo(id); }

void ShowInfo(int item_id){ GameObject itemName_GUI = Instantiate(itemText) as GameObject;

 itemName_GUI.guiText.text = item_name;
 itemName_GUI.guiText.fontSize = 14;
 itemName_GUI.guiText.pixelOffset = new Vector2(-250, 200);

}

void OnGUI() { ScreenWidth = (Screen.width/2); ScreenHeight = (Screen.height/2); int i;

 ArrayList arrayItems = new ArrayList();
 arrayItems.Add("Pie");
 arrayItems.Add("Apple");
 arrayItems.Add("Cookies");
 arrayItems.Add("Chickens");
 arrayItems.Add("Horses");

 int buttonHeight  = -375;


 for (i = 0; i < arrayItems.Count; i++){
     buttonHeight  = buttonHeight+60;
     string name = arrayItems[i] as string;

     if (GUI.Button( new Rect(ScreenWidth-500,ScreenHeight-buttonHeight,250,50), name)){
         StartCoroutine(getItems(i));
         isEmpty = 0;
     }
 }

}

}

Comment
Add comment · Show 2
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 Uzquiano · Apr 24, 2011 at 08:28 PM 0
Share

Hi,

What do you mean exactly with this?

""previous" object is overwritten"

avatar image J-Brinky · Apr 24, 2011 at 08:48 PM 0
Share

What i mean is: When the object is istantiated and the item_name is put in place when the first button is clicked, i want that value to be removed when the 4th button (for example) is clicked. So it only shows up once and not "layer above layer" :)

1 Reply

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

Answer by Uzquiano · Apr 24, 2011 at 09:32 PM

So, what is happening is what you wrote. You are creating a new object every time with this line

GameObject itemName_GUI = Instantiate(itemText) as GameObject;

So what you have to use is create just once that gameObject, and then only update this

itemName_GUI.guiText.text = item_name;

Then the text will change.

My suggestion is:

1 Do this at the very begining

GameObject itemName_GUI = Instantiate(itemText) as GameObject;

Comment
Add comment · Show 4 · 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 J-Brinky · Apr 24, 2011 at 11:13 PM 0
Share

I understand what you are trying to say, i tried putting "GameObject itemName_GUI = Instantiate(itemText) as GameObject;" outside a method but it got me an error - 'error CS0236: A field initializer cannot reference the nonstatic field, method, or property'

Putting it inside let's say Start() doesn't work either, because it can not be accessed. PS: i am writing the code in C#, not Js

avatar image Uzquiano · Apr 24, 2011 at 11:17 PM 0
Share

Yep, you should declare it outside everything, at the verybegining; but then inside the start() you should do the Instantiate()...

And since the reference is rachable for all the script is ok, and yu initialize inside a function, so it works.

It should be something like: GameObject itemName_GUI;

function Start(){itemName_GUI = Instantiate(itemText) as GameObject;}

Sorry but I didn't program in C#

avatar image J-Brinky · Apr 25, 2011 at 01:52 PM 0
Share

Thanks, your comment worked :)

avatar image Uzquiano · Apr 25, 2011 at 05:00 PM 0
Share

Great! Nice to hear ;)

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

How to apply check on an instanced prefab 2 Answers

Two Unity GUI questions 2 Answers

How to make a monolgue? 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