Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate/sending info to all clones. - Solved

Discussion in 'Scripting' started by SimpleGiant, Apr 18, 2015.

  1. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    Its kind of a complex problem, I cant seem to wrap my head around the solution at this time.

    I am making a TD game like Baloons TD. Well working on the wave spawn system i have found a bug, and i cant figure it out.

    this script spawn the minion in the wave and send some much needed information to the new clone.
    Code (CSharp):
    1.     void WaveSpawner(){
    2.         GameObject clone;
    3.         clone = Instantiate (waveMinionArray[waveNumber,spawnCounter],spawnLocation.position, this.transform.rotation)as GameObject;
    4.         clone.GetComponent<Minion>().currentTarget = pathArray[0];
    5.         clone.GetComponent<Minion>().targets = pathArray;
    6.         clone.GetComponent<Minion>().gameManager = gameObject;
    7.  
    8.         spawnCounter ++;
    9.         currentWaveNumber ++;
    10.     }
    I think that the problem is here, somewhere. if i just spawn one minion, it moves threw the game no problem. but when i spawn TWO, the pathfinder gets scrambled for both of them. This tells me that the gamemanager isn't just sending the new clone the information, but sending all the clones the information. resetting the array i am using for the simple pathing.

    I feel like the fix is simple, but i dont see it.
     
  2. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    Just to help everyone see, here is the minion script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Minion : MonoBehaviour {
    5.     public string minionName;
    6.     public bool popped;
    7.     public int baloonValue;
    8.     public Transform[] targets;
    9.     public Transform currentTarget;
    10.     private int targetRayNumber;
    11.     public float moveSpeed;
    12.     public GameObject gameManager;
    13.     //private Vector3 thisObjectsPosition;
    14.  
    15.  
    16.  
    17.     // Use this for initialization
    18.     void awake () {
    19.         popped = false;
    20.         //value check.
    21.  
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         if(popped != true){
    28.             if(transform.position == currentTarget.transform.position){
    29.                 targetRayNumber ++;
    30.                 if(targetRayNumber >= targets.Length){
    31.                     MadeItTheEnd();
    32.                     return;
    33.                 }else{
    34.                     currentTarget.position = targets[targetRayNumber].position;
    35.                 }
    36.             }else{
    37.                 Move();
    38.             }
    39.         }else
    40.         Destroy(gameObject);
    41.     }
    42.     void Move () {
    43.         transform.position = Vector3.MoveTowards(transform.position, currentTarget.position, moveSpeed * Time.deltaTime);
    44.     }
    45.     void MadeItTheEnd(){
    46.         Debug.Log("MadeItTheEnd called");
    47.         gameManager.SendMessage("MadeItTheEnd");
    48.     }
    49.     void Pop(){
    50.         Debug.Log("AddCr called");
    51.         gameManager.SendMessage("AddCr", baloonValue );
    52.         popped = true;
    53.     }
     
  3. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    I figured out my problem. i was moving the path when i wanted to just fallow it.

    Fixed
    Code (CSharp):
    1.                 }else{
    2.                     currentTarget = targets[targetRayNumber];
    3.                 }