Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 no666mi · Apr 07, 2021 at 09:35 PM · unity 2dspawnspawning problems

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?

Comment
Add comment · Show 2
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 DCordoba · Apr 07, 2021 at 01:24 AM 0
Share

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
       */

}

avatar image no666mi DCordoba · Apr 07, 2021 at 11:31 PM 0
Share

Thank you mate ILY

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 }
Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

122 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 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 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 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 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 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

How to increase the spawn rate of an object over time? 2 Answers

How do I make a game object spawn and then stay and move up when mouse is cilcked? 1 Answer

Pickup & Drop Objects + Spawn items HELP 0 Answers

How to spawn multiple prefabs - Tilemap 0 Answers

Increase number of enemies spawned per level 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges