- Home /
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
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;
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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)
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
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
Check edit for an XNA sample. $$anonymous$$aybe that conveys my idea a bit more accuractly
thanks i think I understand and thanks for the script it helped me a lot to understand
Your answer
![](https://koobas.hobune.stream/wayback/20220613110833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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