- Home /
How can i spawn object in different axis using this?
i wanna spawn object with speed from different axis like x -x y and -y
Currently this script is attached to my enemy prefab.
public class meteor : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;
public float rotationSpeed = 100.0f;
public float randomNumber;
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
randomNumber = Random.Range(0, 4);
rb.velocity = new Vector2(0, -speed);
// if (randomNumber == 0)
// rb.velocity = new Vector2(0, speed);
/* if (randomNumber == 2)
rb.velocity = new Vector2(0, -speed);
if (randomNumber == 3)
rb.velocity = new Vector2(speed, 0);
if (randomNumber == 4)
rb.velocity = new Vector2(-speed, 0);*/
}
void Update()
{
transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);
}
};
and this script is attached to my gameobject that instantiate the prefab is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnScript : MonoBehaviour
{
public GameObject asteroidPrefab;
public float respawnTime = 1.0f;
private Vector2 screenBounds;
// Use this for initialization
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(asteroidWave());
}
private void spawnEnemy()
{
GameObject a = Instantiate(asteroidPrefab) as GameObject;
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y*1 );
float randomnumber= GetComponent<meteor>().randomNumber;
/* if (randomnumber == 0)
a.transform.position = new Vector2( Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * -2);
if (randomnumber == 1)
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * 2);
if (randomnumber == 2)
a.transform.position = new Vector2( screenBounds.x * -2,Random.Range(-screenBounds.y, screenBounds.y));
if (randomnumber == 3)
a.transform.position = new Vector2(screenBounds.x * 2, Random.Range(-screenBounds.y, screenBounds.y));*/
}
IEnumerator asteroidWave()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
Now i wanna get a random number ranging number from 1to 4 and than spawn that object from axis
depending on my if condition. i am not able to access random number using get component. Help me?
there is a error and some updates
the error is on line 27
GetComponent<meteor>().randomNumber;
you are calling get component in the objetc that spawns the asteroids, not in the asterioid itself, it should be
a.GetComponent<meteor>().randomNumber;
the updates are,
1) random number is still not set, since you set it on start, this code wont be executed until next frame, so, you need to initialize randomNumber
in spawnScript
the best way is
meteor newMeteor = a.GetComponent<meteor>();
//check if it isnt null
if (newMeteor ){
newMeteor.randomNumber = Random.Range(0, 4);
// do the commented things
}
2) if you dont want to use getComponent you can store the component as prefab and call instantiate over it, this will instantiate the entire prefab but give u the reference of the instantiated component, some like:
public meteor asteroidPrefab;
private void spawnEnemy()
{
//check if you dragged & droped the prefab already
if(!asteroidPrefab){
Debug.Log("Prefab missiong in inspector")
return;
}
meteor aMeteor = <meteor>Instantiate(asteroidPrefab);
aMeteor.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y*1 );
float randomnumber = aMeteor.randomNumber;
/*
the commented things
*/
}
Answer by Rapizer · Apr 08, 2021 at 02:52 PM
The problem is that randomNumber is a float, because floats can store numbers with decimals, chances are that randomNumber will return random numbers that aren't whole (eg. 2.371695). What you should do instead is make the variable an int, as ints only store whole numbers. That way, random.Range will always return a whole number.
//use this instead
public int randomNumber;
Also, you should make it so the variable randomNumber changes every time the spawnEnemy method is called if you don't want all the enemies spawning in the same position. This happens because randomNumber is only called once in the start function, so the random variable will stay the same forever. You can do it like this:
void spawnEnemy()
{
int randomNumber = GetComponent<meteor>().randomNumber;
randomNumber = Random.Range (0, 4);
//do more stuff here
}