- Home /
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!
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?
Your answer
Follow this Question
Related Questions
GUIText Instantiation and Update the Instantiated Text. C# 1 Answer
Accessing Enemy lifebar prefab 1 Answer
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