• 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 D_MC · Apr 05, 2014 at 11:24 AM · c#instantiatetutorialguitextasteroids

GUIText Instantiation to show at Asteroid position

Hi guys,

I've finished going through the Space Shooter/Asteroids tutorial and now I am trying to tweak the game to keep learning.

What I would like is for when the Asteroids are destroyed to have the number of points for that asteroid to be flashed on the screen at the position of the asteroid. I am having some issues with this and I have tried to follow the advice given in this question but I am not getting anywhere!

I have DestroyByTime on the GUIText prefab which I have tested separately and I know will remove the points after a given amount of time but I can't get the text to display where I want it to.

Like I said I have tried to get the code in the linked question to work but I end up crashing the game so I have included a stable version of my code below (I tried to copy how the explosion was Instantiated):

 using UnityEngine;
 using System.Collections;
 
 public class DestroyByContact : MonoBehaviour
 {
     public GameObject explosion;
     public GameObject playerExplosion;
     public GUIText pointsText;
     public int scoreValue;
     private GameController gameController;
 
     void Start ()
     {
 
         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent <GameController>();
         }
         if (gameController == null)
         {
             Debug.Log ("Cannot find 'GameController' script");
         }
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.tag == "Boundary")
         {
             return;
         }
         Instantiate(explosion, transform.position, transform.rotation);
         if (other.tag =="Player")
         {
             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
             gameController.GameOver ();
         }
         else
         {
             Instantiate(pointsText, Camera.main.ScreenToViewportPoint(transform.position), Quaternion.identity);
             Debug.Log ("Position: "+Camera.main.ScreenToViewportPoint(transform.position)+", Rotation: "+Quaternion.identity);
         }
         gameController.AddScore (scoreValue);
         Destroy(other.gameObject);
         Destroy(gameObject);
     }
 }


Lastly! Once I have the GUIText is created I need to set it's text value to scoreValue so any help with how to set the text value on an Instantiated GUIText object would be much appreciated.

Sorry for such a long first question!

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 D_MC · Apr 05, 2014 at 11:15 AM 0
Share

Line 8 declares the public GUIText pointsText Line 40 I try to create the text Line 41 for debugging the position

One thing I noticed was in the debug the x value is always 0 Position: (0.0, 0.0, 2.1), Rotation: (0.0, 0.0, 0.0, 1.0)

Should I have done something similar to the shooting of the laser and have a 'hard point' on the asteroid to spawn the text from?

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

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

C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer

GUIText Instantiation and Update the Instantiated Text. C# 1 Answer

Accessing Enemy lifebar prefab 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges