- Home /
How do i add a gameobject to my array?
Well, i have been trying to work on my code as much as possible, and so far in a day i got a small script to work but i have not been able to add gameobjects i make to a array. and i need to do that.
What i need help is with function Fire with the Adding the gameobject to Clones. var PlayerPos : Vector3; var CameraPos : Vector3; var Player : GameObject; var TruePlayerPos : Vector3; var Cam : GameObject; var Bullet1 : GameObject; var AimPos : Vector3; var VelocityBullet : Vector3; var Clones = new Array(GameObject);
var EnemyGOref : GameObject; var Cube : Mesh;
var EnemyC : GameObject; var Enemys = new Array(GameObject); var CamPos : Vector3; var Agility = 1; var Speed = Agility*2; var Strength; var Endurence; function Start () { Player = GameObject.Find("Player"); Cam = GameObject.Find("Main Camera"); EnemyGOref = GameObject.Find("/EnemyPrefabs/Enemy"); } function Update () { CamPos = PlayerPos; TruePlayerPos = Player.transform.position; if (Input.GetKey("w")) { MoveUp(); } if (Input.GetKey("s")) { MoveDown(); } if (Input.GetKey("a")) { MoveLeft(); } if (Input.GetKey("d")) { MoveRight(); } if (Input.GetKey("up")) { AimPos = TruePlayerPos + 20*Vector3.up; VelocityBullet = Vector3.up; } if (Input.GetKey("down")) { AimPos = TruePlayerPos - 20*Vector3.up; VelocityBullet = -Vector3.up; } if (Input.GetKey("right")) { AimPos = TruePlayerPos + 20*Vector3.right; VelocityBullet = Vector3.right; } if (Input.GetKey("left")) { AimPos = TruePlayerPos - 20*Vector3.right; VelocityBullet = -Vector3.right; } if (Input.GetKeyDown("mouse 0")) { Fire(AimPos,Bullet1,VelocityBullet); } if (Input.GetKey("space")) { Application.CaptureScreenshot("Editing.png"); } if (Input.GetKeyDown("q")) {
}
Player.transform.position=PlayerPos+Vector3(0,0,500);
Cam.transform.position=CamPos;
<pre>
} function Fire (AimPos : Vector3, ProjectileType : GameObject, Velocity : Vector3) : GameObject { Clone = Instantiate(ProjectileType,AimPos,Player.transform.rotation); Clone.transform.position += Velocity; Clones.Add(Clone); } for (Clone in Clones) { Clone.transform.position += VelocityBullet; } function PlaceMine (Pos : Vector3, MineType : GameObject) { Instantiate(MineType,Pos+Vector3(0,0,500),Player.transform.rotation); } function MoveUp () { PlayerPos.y += Speed; } function MoveDown () { PlayerPos.y -= Speed; } function MoveRight () { PlayerPos.x += Speed; } function MoveLeft () { PlayerPos.x -= Speed; } for (Enemy in Enemys) { Distance = Vector3.Distance(Enemy.transform.position,Player.transform.position); EnemyPos = Enemy.transform.position; if (Distance < 25) { EnemyAttack (Enemy); } if (Distance > 25 && Distance < 100) { EnemyRangedAttack (Enemy); } if (Distance > 100) { MoveTowards (Enemy, EnemyPos); } Enemy.transform.position=EnemyPos+Vector3(0,0,500); } function AddEnemy (EnemyList : Array, SpawnPos : Vector3) : GameObject {
} function EnemyAttack (Enemy : GameObject) {
} function EnemyRangedAttack (Enemy : GameObject) {
} function MoveTowards (Enemy : GameObject, Pos : Vector3) { Pos -= Vec3toVec2(PlayerPos); } function Vec2toVec3 (Vec2 : Vector2) : Vector3 { R = Vector3(Vec2.x,Vec2.y,500); return R; } function Vec3toVec2 (Vec3 : Vector3) : Vector2 { R = Vector2(Vec3.x,Vec3.y); return R; }
All you need to do is change your "var Clones = new Array(GameObject);" to "var Clones : GameObject[];"
var Clones : GameObject[]; wont let me do Clones.Add(Clone); says it cant do .Add
Your answer
![](https://koobas.hobune.stream/wayback/20220612181002im_/https://answers.unity.com/themes/thub/images/avi.jpg)