• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lenyeto · Aug 15, 2011 at 08:23 AM · gameobjectarraysadding

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; }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPedersen · Aug 15, 2011 at 08:45 AM 4
Share

Format your code properly, please. :P

avatar image logty · Aug 16, 2011 at 12:50 AM 0
Share

All you need to do is change your "var Clones = new Array(GameObject);" to "var Clones : GameObject[];"

avatar image lenyeto · Aug 16, 2011 at 07:10 AM 0
Share

var Clones : GameObject[]; wont let me do Clones.Add(Clone); says it cant do .Add

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Change Gameobject in Array's position 1 Answer

How to assign random positions to gameobjects from an array? 2 Answers

Array.length edited using scripts 2 Answers

snake problem 0 Answers

Error Assets/OrangeSpawnScript.js(4,33): UCE0001: ';' expected. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges