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 Scribe · Mar 01, 2011 at 05:49 PM · javascriptupdateweaponin-game

making a named script update

Hi there, I recently found a usful script on the unity script web page which gave this script to find the closest enemy to an object:

// Print the name of the closest enemy print(FindClosestEnemy().name);

// Find the name of the closest enemy function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest;
}

I am trying to use an adapt this script for use in a game to find the closest gun/weapon to me. I successfully managed to change the tag that it looks for however unlike this script I need mine to update constantly in-game

my script:

function Update () { FindClosestGun(); print(FindClosestGun().name); }

// Find the name of the closest enemy function FindClosestGun () : GameObject { // Find all game objects with tag Gun var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Gun"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; }

I tried to sort it by just calling the other function through an update script, when I play the game it changes the returned name once but then doesn't change back if I move back towards the original weapon.

I don't get any errors so I don't know why it does this

Thanks in advance

Scribe

Comment
Add comment
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

1 Reply

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

Answer by Proclyon · Mar 01, 2011 at 08:40 PM

This update system in game engines is usually done (meaning , for most engines) with a "hook" (a general term for putting things together). To hook your stuff to the Unity3D update routine you need to make your script derive from monobehaviour. If you do not do this you need to let an object that does do this forward each frame a call to an update inside the script in which that IS done.

Similarly done in XNA is the Game class or GameComponent/DrawableGameComponent which have an update method. If a class does not derive from Game it needs to have an update method which is called via some object reference from some class that is on the highest level of the chain all being called with the original update routine.

THIS IS A SAMPLE OF XNA CODE AS A REPLY ON A COMMENT IT WILL NOT FUNCTION IN UNITY3D AND SERVES NO OTHER PURPOSE BUT TO EXPLAIN A CONCEPT

//Some dependancies and library stuff using 'stuff';

public class Main : Game { //constructors load unload content draw and what not junk;

 /// &lt;summary&gt;
 /// Allows the game to run logic such as updating the world,
 /// checking for collisions, gathering input, and playing audio.
 /// &lt;/summary&gt;
 /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt;
 protected override void Update(GameTime gameTime)
 {
     someObjectReference.Update()

     base.Update(gameTime);
 }

}

someObjectReference (your other script) would now be able to receive this once per update. The same can be done if you skip this and just derive that object from the Game class ( Or GameComponent OR DrawAbleGameComponent which are parts of the Game class which is the root updater)

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 Scribe · Mar 02, 2011 at 05:40 PM 0
Share

not familiar with XNA and not great at scripting either. Are you saying I should make a monobehaviour and call my other script, If at all possible could you give an example of how to do this

avatar image Proclyon · Mar 03, 2011 at 07:52 PM 0
Share

I'm afraid that will not help you much, i've been out of the loop with Unity3D for quite a while now and I'd probably make it a problem before a guide. Basically you need to "pass on" the update routine. Doing that you need to connect it to the root update or a branch with that update. In unity that root is monobehaviour. So either connect the script to a monobehaviour or script that derives from it in some connection and call a method from in that class which by doing so keeps calling that via via. I hope this makes any sense at all :/ Sorry for not being able to provide a sample

avatar image Proclyon · Mar 03, 2011 at 07:53 PM 0
Share

Check edit for an XNA sample. $$anonymous$$aybe that conveys my idea a bit more accuractly

avatar image Scribe · Mar 12, 2011 at 05:29 PM 0
Share

thanks i think I understand and thanks for the script it helped me a lot to understand

avatar image Proclyon · Mar 13, 2011 at 10:20 PM 0
Share

Great to hear :) Good luck with your project!

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

How to assign multiple EditorApplication.update delegates in JS? 2 Answers

Problem with restarting the scene (js) 2 Answers

Weapon swapping in-game 2 Answers

Why doesn't this weapon switching script work 1 Answer

best way to weapon swapping 0 Answers


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