1
votes

The title pretty much sums it up. My enemy prefab is only being instantiated in the Y-axis but the coin prefab is being instantiated how I wanted it, even though they have both the same coordinates.

import System.Collections.Generic;

var enemyPrefab : GameObject;
var coinPrefab : GameObject;
public var Coinnumber = 1;
public var Coinnumberside : float = 0.0f;

   function OnTriggerEnter(other: Collider)
     {
    if (other.tag == "Trigger" )
    {       
        var StagePosition : Vector3;
        StagePosition.z = 5.69 + Coinnumber;
        StagePosition.x = 0.0 + Coinnumberside;
        StagePosition.y = 0.4;

        Instantiate(coinPrefab, StagePosition, Quaternion.identity);

    Coinnumber = Coinnumber + Random.Range(5,10); 
    Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);



        var EnemyPosition : Vector3;
        EnemyPosition.z = 5.69 + Coinnumber;
        EnemyPosition.x = 0.0 + Coinnumberside;
        EnemyPosition.y = 0.4;

        Instantiate(enemyPrefab, EnemyPosition, Quaternion.identity);

    Coinnumber = Coinnumber + Random.Range(5,10); 
    Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);   




    }
 }
2
There's something wrong in your question...they don't have the same coordinates. x and z axis depend upon variables that are changed between the 2 assignments, in an aleatory way. Maybe Coinnumber and Coinnumberside have to be changed only at the end of the function?Andrea
Besides what others already pointed, what about you object pivot point, global and local. Are they at the right object point and direction?VicM

2 Answers

0
votes

Unless I've misunderstood you, you're trying to create an enemy at the same position as you instantiate a coin. The issue, as I can see it, are these lines:

Coinnumber = Coinnumber + Random.Range(5,10); 
Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);
...
EnemyPosition.z = 5.69 + Coinnumber;
EnemyPosition.x = 0.0 + Coinnumberside;

You're recalculating the Coinnumber variable right before you set the enemy position. The only unchanging value is the Y coordinates, since you recalculate both Z and X. So, for example, the first time you run this script, your coin will spawn at the position (5.69f,0.4f,0f) and then enemyPosition will get random values added to this, so it could (for example) be (10.69f,0.4f,0.2f).

Consider perhaps trying this:

var StagePosition : Vector3;
StagePosition.z = 5.69 + Coinnumber;
StagePosition.x = 0.0 + Coinnumberside;
StagePosition.y = 0.4;

Instantiate(coinPrefab, StagePosition, Quaternion.identity);
Instantiate(enemyPrefab, StagePosition, Quaternion.identity);

Coinnumber = Coinnumber + Random.Range(5,10); 
Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);

This will spawn the enemy at the same position as your coin, and then afterward calculate a new position for your next coin & enemy. If however this isn't what you're trying to do, let me know and I'll change my answer.

0
votes
import System.Collections.Generic;

var enemyPrefab : GameObject;
var coinPrefab : GameObject;
public var Coinnumber = 1;
public var Coinnumberside : float = 0.0f;
public var Enemynumber = 1;
public var Enemynumberside : float = 0.0f;

function OnTriggerEnter(other: Collider)
{
    if (other.tag == "Trigger" )
    {       
        var StagePosition : Vector3;
        StagePosition.z = 5.69 + Coinnumber;
        StagePosition.x = 0.0 + Coinnumberside;
        StagePosition.y = 0.4;

        Instantiate(coinPrefab, StagePosition, Quaternion.identity);

        Coinnumber = Coinnumber + Random.Range(5,10); 
        Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);
    }

    if(other.tag == "Trigger" )
    {
        var EnemyPosition : Vector3;
        EnemyPosition.z = 5.69 + Enemynumber;
        EnemyPosition.x = 0.0 + Enemynumberside;
        EnemyPosition.y = 0.4;

        Instantiate(enemyPrefab, EnemyPosition, Quaternion.identity);

        Enemynumber = Enemynumber + Random.Range(5,10); 
        Enemynumberside = Enemynumberside + Random.Range(0.2,-0.4);

    }
}

I'm still not pretty sure why this worked after I seperated them but it did, so I'm putting it as an answer. Thanks for everyone who answered and tried to solve my problem, I really appreciate it.