Melin Ven Melin Ven - 3 months ago 17
C# Question

Instantiate score flash at a specific position

I am facing now a problem that I hope that you can help me solve it. I have a space shooter game where I added score flash once you destroy a

gameobject
. The thing I want to correct is that I don't know how to spawn the score flash at the place where the
gameobject
was destroyed. It is spawning always in one place where I have set it to
spawn
but I want it to spawn always at the place where the
gameobject
gets destroyed.
Here is the code:

public GameObject explosion, spawnItem;
public GameObject playerExplosion;
private Done_GameController gameController;
private PlayerHealth playerhealth;
public Text ScoreFlash;
public GameObject canvasg;
public int scoreValue;
public float lifetime;

void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Boundary" || other.gameObject.tag == "Enemy")
{
return;
}
if (other.gameObject.tag == "Bullet")
{
playerhealth.AddScore (scoreValue);
Destroy (gameObject);
scoreflash ();
}
if (other.gameObject.tag == "Player" || other.gameObject.tag == "Bullet")
{
Instantiate (explosion, transform.position, transform.rotation);
Destroy (gameObject);
}
if (CheckIfAsteroid (transform.tag, 3) == "Ast" && other.gameObject.tag == "Bullet")
{
Instantiate (spawnItem, transform.position, Quaternion.identity);
}
}
string CheckIfAsteroid(string tag, int index)
{
Debug.Log (tag.Substring (0, Mathf.Min (tag.Length, index)));
return tag.Substring (0, Mathf.Min (tag.Length, index));
}
void scoreflash()
{
ScoreFlash.text = " +" + scoreValue + "p";
Text newtext = Instantiate(ScoreFlash) as Text;
GameObject newcanvas = Instantiate (canvasg) as GameObject;
newtext.transform.SetParent (newcanvas.transform,false);
Destroy (newcanvas,lifetime);
}


So the
scoreflash
function is the function that instantiates the text with the needed value and the string Check if asteroid part is for the instantiation of a gameobject. Where the gameobject will be instantiated there the text needs to appear. I have deleted a part of the hole code because it is not important. Please Help me!

Answer

I don't know how to spawn the score flash at the place where the gameobject was destroyed

Get the position of that GameObject before destroying it then call the scoreflash function and pass in the position into it.

Replace the OnTriggerEnter and scoreflash function with the new function below. You may need to modify it to work correctly but this should get you started.

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Boundary" || other.gameObject.tag == "Enemy")
    {
        return;
    }
    if (other.gameObject.tag == "Bullet")
    {
        playerhealth.AddScore(scoreValue);

        //Get position before destryoing gameobject
        Vector3 diedPos = gameObject.transform.position;
        Destroy(gameObject);
        scoreflash(diedPos);
    }
    if (other.gameObject.tag == "Player" || other.gameObject.tag == "Bullet")
    {
        Instantiate(explosion, transform.position, transform.rotation);
        Destroy(gameObject);
    }
    if (CheckIfAsteroid(transform.tag, 3) == "Ast" && other.gameObject.tag == "Bullet")
    {
        Instantiate(spawnItem, transform.position, Quaternion.identity);
    }
}

void scoreflash(Vector3 diedPosition)
{
    ScoreFlash.text = " +" + scoreValue + "p";
    Text newtext = Instantiate(ScoreFlash) as Text;
    GameObject newcanvas = Instantiate(canvasg) as GameObject;
    newtext.transform.SetParent(newcanvas.transform, false);

    Vector3 screenPos = Camera.main.WorldToScreenPoint(diedPosition);
    newtext.transform.position = screenPos;
    Destroy(newcanvas, lifetime);
}
Comments