Search Unity

Space Shooter Upgrade Issues

Discussion in 'Scripting' started by tburns517, Feb 9, 2016.

  1. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    I'm upgrading the Space Shooter tutorial provided by Unity.

    Problem 1
    I would like to make it so when an Asteroid is shot, two other, smaller, asteroids spawn next to it. What sort of script can I write to do so? I was researching and saw you could change the scale of a prefab, so I was thinking I could do something like that.

    Problem 2
    I'm at a loss with this one. I've spent countless hours trying to figure out what is wrong. I have a pick up object that the player picks up. In order to make this work so the object doesn't get destroyed by the Boundary, I set the pick up object's render to false and the player carries the object until it is to be destroyed. That is all in the ShieldPickUp script. the ShieldControl script that I have controls the shield object, which I have made a prefab. A bool value in ShieldPickUp is set and will notify the ShieldControl to spawn a new shield at the player position. Once this shield gets hit, it's destroyed. The problem is a new shield will not spawn.

    ShieldPickUp


    Code (CSharp):
    1.     public static bool shieldPickUp;
    2.     GameObject playerPosition;
    3.  
    4.     void Start()
    5.     {
    6.         playerPosition = GameObject.Find("Player");
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         if (shieldPickUp == true && playerPosition != null)
    12.         {
    13.             gameObject.transform.position = new Vector3(playerPosition.transform.position.x, 0.0f, playerPosition.transform.position.z - 1);
    14.         }
    15.     }
    16.  
    17.     void OnTriggerEnter(Collider other)
    18.     {
    19.         if (other.tag == "Player")
    20.         {
    21.             shieldPickUp = true;
    22.             gameObject.GetComponentInChildren<Renderer>().enabled = false;
    23.         }
    24.     }

    ShieldControl


    Code (CSharp):
    1.     GameObject playerPosition;
    2.  
    3.     void Start()
    4.     {
    5.         playerPosition = GameObject.Find("Player");
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         if (ShieldPickUp.shieldPickUp == true && playerPosition != null)
    11.         {
    12.             Instantiate(gameObject, new Vector3(playerPosition.transform.position.x, playerPosition.transform.position.y, playerPosition.transform.position.z), Quaternion.identity);
    13.             transform.position = playerPosition.transform.position;
    14.         }
    15.     }
    16.  
    17.     void OnTriggerEnter(Collider other)
    18.     {
    19.         if (other.tag == "Asteroid" || other.tag == "Enemy" || other.tag == "BossBolt")
    20.         {
    21.             Destroy(other.gameObject);
    22.             Destroy(gameObject);
    23.         }
    24.     }
    Any help for any of the problems that I have listed will be greatly appreciated!
     
    Last edited: Feb 9, 2016
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    1) you can get a reference to the gameobject you just instantiated, then set it's local scale

    Code (csharp):
    1.  
    2. GameObject miniAsteroid = Instantiate(gameObject, transform.position, Quaternion.identity); // make a clone of itself
    3. miniAsteroid.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    4.  
    might want to set the rotation to some random orientation etc.