Search Unity

How to grab and move all instantiated objects?

Discussion in 'Scripting' started by Anon1234, Apr 26, 2015.

  1. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    I tried to instantiate an GameObject already placed in scene. So I have Bullet, which is child of Player, player has this script attached. I dropped Bullet into the slot. I'd like to... actually, the script speaks for itself. I can't start game, so I don't even know if I fail at "GameObject theClonedBullet" or at "theClonedBullet.transform". I would like to execute theClonedBullet.transform.position += transform.right / 10; on every bullet instantiated. Is there way to grab them all? (there will be around 200 of them if not more).

    Assets/Scripts/Player/FireBullet.cs(15,36): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FireBullet : MonoBehaviour {
    5.     public GameObject Bullet;
    6.  
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         if (Input.GetMouseButton (0)) {
    14.             Vector2 originalPosition = new Vector2(Bullet.transform.position.x, Bullet.transform.position.y);
    15.             GameObject theClonedBullet = Instantiate(Bullet, new Vector3(0, 0, 0), Quaternion.identity);
    16.  
    17.             theClonedBullet.transform.position += transform.right / 10;
    18.         }
    19.     }
    20. }
    21.  
    For my defense, I was Googling it, hence the middle line, but then it complains about something else.
     
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Change this:
    Code (csharp):
    1. GameObject theClonedBullet =Instantiate(Bullet, newVector3(0, 0, 0), Quaternion.identity);
    into this:
    Code (csharp):
    1. GameObject theClonedBullet =(GameObject)Instantiate(Bullet, newVector3(0, 0, 0), Quaternion.identity);