• 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
Question by Dartmor · Dec 10, 2014 at 06:27 PM · guiinstantiatetext

Difference between a script in the scene and in instantiated

I have a script about GUIText.

 public GUIText Enemyhealth;
 private int EnemyH = 300;
 private float distance = 2f;
 
 Start {
 Enemyhealth.text = " " + EnemyH;
 }
 
 Update {
 
 if (Vector.Distance(transform.position, Vector.zero) < distance)
 {
 EnemyH = EnemyH - 50;
 Enemyhealth.text = " " + EnemyH;
 }
 


And it works perfectly - when both Enemyhealth ( GUIText object ) and object ( to which the script is attached to ) are on the scene.

But when i try to simply add "GUIText EnemyHealth = Instantiate ( Enemyhealth ) as GUIText;" trying to actually instantiate both GUIText and the object ( to which script is attached to ), it don't work.

  1. It shows GUIText , but with a state from the last play before. If the last time i played it was 250, then only the next time i play it shows 250.

  2. And it don't change the value in the game itself. If 250 should become 200, it stills shows 250, while in prefab it actually changed to 200. But the change itself will be shown only the next time i play. Without changing it in play mode at all.

So, as i said, it works perfectly when both GUIText object and the object ( to which scrpit is attached to ) are in the scene, but it's broken when they are instantiated. Why?

Comment

People who like this

0 Show 0
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

Answer by thepenguinmaster · Dec 10, 2014 at 06:40 PM

To get a reference to the script you will want to do the following:

instantiate as a gameobject gameobject.getcomponent

I had the same problem at one point. This is just psuedo code, but you have all of the parts there, you just need to arrange it differently, and when instantiating, get the game object, and then pull the script component out of the newly created gameobject. from there you can update the values in Enemyhealth.

Not sure if this helps, but from what I understand of your code and question, this may solve the problem.

Comment
Dartmor

People who like this

1 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 Dartmor · Dec 10, 2014 at 09:45 PM 0
Share

Explain it a bit more. I need to create a script in the scene, and then GetComponent ( this script ) from an instantiated object?

avatar image Dartmor · Dec 10, 2014 at 11:07 PM 0
Share

Anyway i think i've founded a solution. I've created empty object, attached a sript to it ( that changes value ), created the GUIText object in the scene and dragged it to the script ( in the scene, under empty gameobject ). Added a collider to it ( the same size as the main object that triggers changes ). And just made the main object ( in main object's script) to become parent of this empty object in the scene. So empty object with script moves with the main object, having the same size of collider. The thing is - i don't need to instantiate nor the script nor the enemy health GUIText object, because there are already there in the scene, just becoming child of my instantiated main object :D

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

26 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

Related Questions

Instantiating GUI text after 2nd object is destroyed 1 Answer

GUI OnMouseEnter popup. text String is layering for multiple game objects? 1 Answer

I am having issues with my textmesh shader 0 Answers

Help with making a triggered gui message 1 Answer

GUI using custom graphics - GUI approuches 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