Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Freel4tte · Apr 28, 2011 at 01:10 PM · javascriptarray

Push once then keep updating.

As the title speaks for itself, I'll explain it more properly.

I have an array wich contains positions of certain objects. I don't want to keep pushing more and more of the same objects in the array so I put the push in a Start function. But now the positions in the inspector (from a Vector3 array wich is filled with the normal array) arentio't changing. Obvious cause its in a Start function.

Now for the question: How can I push these objects in the array, and then keep the positions updating?

Help is really appreciated :) Thanks in advance!

For those who need the script:

//This script is created and made by Freel4tte. //Anyone who uses this script illigal or without permission will //get hunted down by zombies on tricycles and ninja bears with laser eyes. //You have been warned.

var filteredEnemies = Array(); //all other objects var posEnemies = Array(); //positions of other objects

var showEnemies : GameObject[]; var getPos : Vector3[];

@HideInInspector var enemies : GameObject[];

function Start(){ seeEnemies(); }

function Update(){

 getPos = posEnemies;

}

function seeEnemies():GameObject{

 enemies = GameObject.FindGameObjectsWithTag("enemy");

     for (var currEnemy in enemies){

         if (currEnemy == gameObject)
         continue;

         filteredEnemies.Push(currEnemy);
         posEnemies.Push(currEnemy.transform.position);

     }

 showEnemies = filteredEnemies;

}

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 Joshua · Apr 28, 2011 at 01:14 PM 0
Share

Long question short: if the seeEnemies function produces a NEW enemy you want it to be pushed into the array, if it's not NEW you want nothing to happen. Correct?

avatar image Freel4tte · Apr 28, 2011 at 01:20 PM 0
Share

I don't really get your comment. Do you mean when an enemy is spawned in runtime?

avatar image Joshua · Apr 28, 2011 at 01:31 PM 0
Share

Is it that when ever currEnemy is different you want it to be pushed into the array - or is it that at start you want the currEnemy to be pushed into filteredEnemies - but posEnemies get's pushed constantly?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Joshua · Apr 28, 2011 at 01:35 PM

If I understand your question correctly, in your update function you want to loop through the array of filteredEnemies and update the posEnemies array with the new positions?

if so, in the Update function:

for(i=0;i<filteredEnemies.length;i++) {

 posEnemies[i] = filteredEnemies[i].transform.position

}

Comment
Add comment · Show 5 · Share
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 Joshua · Apr 29, 2011 at 09:22 AM 0
Share

That's what this does. In the update function it constantly changes the array of enemypositions to their current position..

avatar image Freel4tte · Apr 29, 2011 at 09:25 AM 0
Share

This doesnt work. The positions are pushed in the array when the game starts. This happens only once because if it where in update, the array will get more and more of the same positions. When the game starts it checks for the already placed enemies in game, and stores them in an array. An other array gets the positions of these objects. Right now there are three of them. But I want to update this position array so I can track the exact position in the inspector.

What I'm trying to make here is when an enemy comes near an position in the array, it stays a couple of units away from it. Thanks

avatar image Joshua · Apr 29, 2011 at 09:50 AM 0
Share

Why doesn't this work? o.O the positions in the position array are being replaced with the current positions, as you wanted? Do you also need to check for need enemies?

avatar image Joshua · Apr 29, 2011 at 09:51 AM 0
Share

for new enemies*

avatar image Freel4tte · Apr 29, 2011 at 11:16 AM 0
Share

Never$$anonymous$$d... Your way works. I forgot to give the array in the inspector the values of the posEnemies. Thanks for your time and help my man!

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

"For"-Loop only works for final element in array 1 Answer

Javascript How to Activate/Deactivate GameObject Arrays 1 Answer

working with array index... 1 Answer

InvalidCastException error and logic error 1 Answer

How to use Javascript Array with classes? 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