• 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 Zephan92 · Jan 04, 2014 at 07:37 AM · c#instantiateprefabguitext

C#, How do I make the GUIText Show up if Instantiate from a prefab.

I have been trying to figure this out all day.

My goal is to instantiate a GUIText prefab. I am trying to make all of my scripts use private calls for GameObjects instead of having to drag them into the inspector in Unity. The problem is though, it seems that I lose my clone reference as soon i leave the Start().

To be more specific, I am trying to call a GUIText called "Pause Text" and have the text show up on the screen when I pause my game. The way I have it now apparently works. I have Debug.Logs saying what the GUIText.text is and it alternates between "" and "PAUSED". BUT, It doesn't show up at all in the camera view.

Here is my code, hopefully somebody can make heads of what I want. Thanks in advance!

 private GameObject createObject;
 private GUIText pauseText;
 private bool paused = false;
 
 void Start() {
         createObject = Resources.Load("Pause Text") as GameObject;
         pauseText = createObject.guiText;
         pauseText.text = "";
 }
 
 void Update () {
 if (Input.GetKeyDown(KeyCode.KeypadEnter))
         {
             paused = !paused;
             if (paused == true)
             {
                 pauseText.text = "PAUSED";
                 Time.timeScale = 0;
             }
             else if (paused == false)
             {
                 pauseText.text = "";
                 Time.timeScale = 1;
             }
         }
 }

I tried instantiated the GUIText object, but that always gave null references, and it didn't seem to work in the first place.

Thanks!

Comment
Add comment · Show 1
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 Zephan92 · Jan 04, 2014 at 07:53 PM 0
Share

Just curious, is this the only way to bump questions?

1 Reply

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

Answer by valim · Jan 04, 2014 at 08:16 PM

 createObject = Instantiate(Resources.Load("Pause Text"), Vector3.zero, Quaternion.identity) as GameObject;

Resource.Load doesn't instantiate the object.

Code not tested


Why you instantiate a guiText? Why don't you let the guiText in the scene with blank text and get that object instead of spawning one.

Comment
Add comment · Show 2 · 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 Zephan92 · Jan 04, 2014 at 08:46 PM 0
Share

Oh hadn't thought of that. Let me test it.

avatar image Zephan92 · Jan 04, 2014 at 08:51 PM 0
Share

I changed the line to this,

createObject = GameObject.Find("Pause Text");

and added pause text to the scene. I guess I was trying to avoid adding it to the scene. But since it is a startup object, I should just leave it in.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

setting the text of an instantiated prefab's child's guitext object 1 Answer

Instantiate prefab (button, texture) with attached script 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

Object after instantiate doesn't apply AddForce 1 Answer

instantiate prefab assigned in inspector c# 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