Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Spawning sphere (waypoints)

Discussion in 'Scripting' started by spearbeard, May 7, 2018.

  1. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    OK I have made a spawning system that currently makes the sphere spawn on the way points but they do not currently move on the z axis. I would like to get them moving to the other way points but also have it so that they move to other points in the array. Here is what i have done so far.

    3 scripts are involved
    Code (CSharp):
    1. public class BallObstacleSpawner : MonoBehaviour {
    2.  
    3.     public Transform[] spawnPoints; //  make a Transfromation variable array for the spawnPoints;
    4.  
    5.     public GameObject ballPrefab; // sets gameobject called ballprefab
    6.  
    7.     public float timebetweenwaves = 2f; // variable for when the balls spawns on the spawner
    8.  
    9.     private float timeToSpawn = 1.8f; // Private variable for when the balls spawns on the spawner
    10.  
    11.     void SpawnBalls() // spawnballs function
    12.     {
    13.  
    14.         int randomIndex = Random.Range(0, spawnPoints.Length); // a int randomIndex that equals a set random range from the current position to the length between spawnpoints
    15.  
    16.         for (int i = 0; i < spawnPoints.Length; i++)// if the int in the index is not equal to 1 then creates a clone of the object int he spawn position to move with the same trasnformations
    17.         {
    18.             if (randomIndex != 1)
    19.             {
    20.                 Instantiate(ballPrefab, spawnPoints[i].position, Quaternion.identity);
    21.             }
    22.         }
    23.     }
    24.  
    25.  
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.  
    30.  
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update () {
    35.  
    36.        
    37.  
    38.  
    39.         if(Time.time >= timeToSpawn)
    40.         {
    41.           SpawnBalls(); // referance to spawnballs function. The spawnballs function gets called
    42.             timeToSpawn = Time.time + timebetweenwaves; //
    43.         }
    44.  
    45.     }
    46. }
    47.  
    48. public class MovingBall : MonoBehaviour // Script that deals with the balls movement from points
    49. {
    50.  
    51.     public float speed = 7.5f;
    52.     private Transform target;
    53.     private int wavepointIndex = 0;
    54.  
    55.     // Use this for initialization
    56.     void Start ()
    57.     {
    58.         target = Waypoints.points[0];
    59.     }
    60.    
    61.     // Update is called once per frame
    62.     void Update ()
    63.     {
    64.         Vector3 dir = target.position - transform.position;
    65.         transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    66.  
    67.         if (Vector3.Distance(transform.position, target.position) <= 0.4f)
    68.         {
    69.             GetNextWaypoint();
    70.         }
    71.     }
    72.  
    73.     void GetNextWaypoint()
    74.     {
    75.         if (wavepointIndex >= Waypoints.points.Length -1)
    76.         {
    77.             Destroy(gameObject);
    78.             return;
    79.         }
    80.  
    81.             wavepointIndex++;
    82.         target = Waypoints.points[wavepointIndex];
    83.     }
    84.  
    85. }
    86.  
    87. public class Waypoints : MonoBehaviour {
    88.  
    89.     public static Transform[] points; // static Transfromation array that stores points
    90.  
    91.     private void Awake() // awake function
    92.     {
    93.         points = new Transform[transform.childCount]; //points are given a new transformation. all the children of the parent object inherit the same destination to go to
    94.         for (int i = 0; i < points.Length; i++)
    95.         {
    96.             points[i] = transform.GetChild(i); // find a child of the parent and makes it spawn at the next point in the array
    97.         }
    98.     }
    99. }
    100.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That is what you'd like to happen, so what is actually happening / not happening that needs fixing?

    Have you checked in the debugger that the waypoints all have their z components set properly?

    Also, you could consider removing line 64 (setting dir) and instead, after line 69 (getting next waypoint), just do
    transform.LookAt( target )
    .

    Then your line 65 could become
    transform.position += Vector3.forward * Time.deltaTime * speed;
    .

    That would save calculating the dir each frame (assuming the waypoints aren't moving).
     
  3. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    A sphere game object spawns at a way point and currently does not move and then another spawns on top of the same spot. I had it before where they would spawn and move then go to the first way point being the parent way point now they all just spawn on children but do not move. Apologies if I did not explain right.

    Each block of code is its own script there not all in the same one
     
    Last edited: May 7, 2018
  4. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    the error problems are IndexOutofRangeException: array index is out of range for this line
    Code (CSharp):
    1. void Start ()
    2.     {
    3.         target = Waypoints.points[0];
    4.     }
    an the other error is NullReferanceException: Object not set to an instance of an object

    Code (CSharp):
    1.  Vector3 dir = target.position - transform.position;
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ah, that might have been useful information to include initially. :)

    So in line 89, have you set these in the Editor? It sounds like they may have not been added.
     
  6. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    this is what the script is
    Code (CSharp):
    1. public class Waypoints : MonoBehaviour {
    2.  
    3.     public static Transform[] points; // static Transfromation array that stores points
    4.  
    5.     private void Awake() // awake function
    6.     {
    7.         points = new Transform[transform.childCount]; //points are given a new transformation. all the children of the parent object inherit the same destination to go to
    8.         for (int i = 0; i < points.Length; i++)
    9.         {
    10.             points[i] = transform.GetChild(i); // find a child of the parent and makes it spawn at the next point in the array
    11.         }
    12.     }
    13. }
    14.  
    apologies if i'm not explaining this properly
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    No need to apologise. :)

    If you are setting points inside the class, there is no need to give it a public modifier (line 3 in the last post). That's why I thought it was being set in the Editor.

    Have you checked that line 10 is actually being executed?
     
  8. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    line 10 does execute but they spawn and due to the collision fly up. If i remove public it won't be able to call the transform array. I have been watching a tutorial and merged 2 styles of spawning both in the tutorials use way points. (brackeys) channel. one is for spawning in a sequence and the other was spawning in a array it might be that the arrays are conflicting
     
    Last edited: May 7, 2018
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, let's back this van up a little - above it was stated that the spheres were not moving and we had a null reference. Now we have a number of them spawning and moving due to a collision. :)

    Let's try this - just remove all spheres except for one. Allow that to spawn. What happens - does it move as expected or not?
     
  10. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    Even without a sphere in the scene or hierarchy they still spawn as they have been set to spawn as game objects on the spawn points which is set up on a game object. The first load of sphere spawn and appear stationery then when the next load of spheres are spawned they shoot up woulds as they spawn either inside the previous sphere or on top.
     
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, so I have just dropped your 3 original scripts into an empty project. I created 2 waypoints (so 2 spheres were generated). The only change I made was I commented out
    BallObstacleSpawner.Update()
    and I created
    BallObstacleSpawner.Start() {SpawnBalls();}


    2 spheres were created and moved along the waypoint lines which I guess is the expected result.

    So presumably, there is something else in your project that is causing you problems. Maybe try commenting out things until you find the source of your problem.
     
  12. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    I will see what I can find out. thank you