Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Hugo12343 · Oct 28, 2017 at 01:06 AM · scripting problemprefabscript.text

How can I apply a text variable to a prefab

Hello,

I have a script which updates on screen text. This script is attached to a coin gameObject which is a prefab in my scene. The script is below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Coin : MonoBehaviour {
 
     //coins
 
     [HideInInspector] public static int coinCount;
 
     public Text coinText;
 
     void Awake() {
 
         coinCount = 0;
 
     }
 
     void OnTriggerEnter2D (Collider2D other) {
 
         if (other.gameObject.CompareTag ("Player")) {
 
             //gameObject.SetActive(false);
 
             Destroy (gameObject);
 
             //coin counter
 
             coinCount = coinCount + 1;
 
             Debug.Log ("Coins collected: " + coinCount);
 
             //update text
 
             coinText.text = "Coins: " + coinCount;
 
         }
 
     }
 
 }

My problem is that Unity will not allow me to apply the text variable to the prefab. When I drag it in to the space where the variable is, it stays bold after I hit apply (to apply it to the prefab).

Why will it not let me apply it to the prefab?

Thanks in advance

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 Hellium · Oct 28, 2017 at 08:53 AM 0
Share

Where does the coinText gameobject comes from? If you drag&drop it from the scene, then, it's normal. A prefab (which is an asset file) can't store a reference of an object belonging to the scene.

avatar image Hugo12343 Hellium · Oct 29, 2017 at 12:33 AM 0
Share

Yes I am just dragging and dropping it. And now I understand why it's not applying (because it is only specific to the scene. So how can I make the text object a global asset?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TanselAltinel · Oct 28, 2017 at 02:58 PM

Unity's behavior is correct in this situation. Your Text object is on the scene, however your prefab is independent on the scene.

You are basically trying to assign a scene dependent object to a scene independent object. A basic question would be that, what will happen when you use that prefab in a scene where there is no text objects in the scene?

Your solution would be that either keep that prefab specific to that scene and do not worry about the variable being bold / unsaved, or better, find the Text object in the scene via script with name or type at the beginning.

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 Hugo12343 · Oct 29, 2017 at 12:29 AM 0
Share

So how can I assign the text object via a script rather than dragging and dropping it.

I think it's something along the lines of

 void Awake () {
 
      coinText = GetComponent<Text> ();
 
 }

but I'm not sure. Can you tell me how to do it

avatar image
1

Answer by Hugo12343 · Oct 29, 2017 at 12:45 AM

Ahh I figured it out. I just made the text object (not the canvas just the text) a prefab. Then dragged that as an asset in. But thanks @TanselAltinel I didn't realise that the gameObject had to be global

Comment
Add comment · 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

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

144 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 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 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 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 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 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

Can't drag text from hierarchy with TextMeshPro 0 Answers

Save GameObject to file without Playerpref 0 Answers

Multiple objects being destroyed, count for them being destroyed doesnt go up past once. 2 Answers

Save GameObject to file without Playerpref 2 Answers

SetActive() not working 0 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