Question by 
               shiranova · Dec 01, 2020 at 12:01 PM · 
                transformarrayarraystransform.positionarray of gameobjects  
              
 
              Moving spawned enemies randomly towards several pre-defined positions
Hi everyone, I am new to Unity so it's probably something simple, but so far could not find a solution anywhere.
I am creating a game where I spawn a new enemy every 2 seconds. Now, I want that each newly spawned enemy will go to 1 possible position out of 600. The final result should be that each enemy will go randomly to one to the predefined spots on the screen.
I can't drag the spots in the inspector to the array because Unity does not let me. And I also tried adding a foreach before, but it did not work as well. What am I doing wrong? Thanks!
I tried:
 private Rigidbody2D rb;
     private GameObject[] spots;
     private float moveSpeed;
     private Vector3 directionToPlayer;
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         spots = GameObject.FindGameObjectsWithTag("spot");
         moveSpeed = 2f;
   
     }
 
 
     private void FixedUpdate()
     {
         MoveEnemy();
     }
 
 
    private void MoveEnemy()
     {
         int rand;
         rand = Random.Range(0,spots.Length);
         directionToPlayer = (spots[rand].transform.position - transform.position).normalized;
         rb.velocity = new Vector2(directionToPlayer.x, directionToPlayer.y) * moveSpeed;
     }
Thank you very much!
               Comment
              
 
               
              Answer by kmhworking · Jan 20, 2021 at 11:47 AM
You have all your variables in private, replace them with public.
public Rigidbody2D rb; public GameObject[] spots;
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                