Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bishop87 · Oct 29, 2016 at 05:46 AM · c#namespacereturn value

Returning Values from custom namespaces C#

Hey guys, this is a bit of a throwback to a script that I had asked about maybe a week ago, so apologies if this seems repetitive.

I have a number of different scripts that, because of a variety of reasons, all tend to use identical or nearly identical search functions of game objects (Find closest target of specific type, Find target with lowest health of specific, find random target of specific type, etc). The end result is that I have an increasing number of scripts that are starting to run essentially identical functions within them. Rather than copying and pasting these functions, I considered it'd be far more efficient (and easier on the eyes) to just pool these functions in a custom namespace so that I could access them easier and with less lines of code.

Here's an example of the code(s) I'm working with.

(Master Script containing the functions)

 namespace MasterScript
 {
     public class FindClosestTarget : MonoBehaviour
     {
 
         public static GameObject findclosesttarget(string a, Transform thisobject)
         {
             GameObject[] gos = GameObject.FindGameObjectsWithTag(a);
             float distance = Mathf.Infinity;
             Vector3 position = thisobject.transform.position;
             foreach (GameObject go in gos)
             {
                 Vector3 diff = go.transform.position - position;
                 float curDistance = diff.sqrMagnitude;
                 if (curDistance < distance)
                 {
                     if (go.GetComponent<Life>() != null)
                     {
                         distance = curDistance;
 
                         return go;
                     }
                 }
             }
             return null;
         }    
     }
 }

And here is an example of trying to call the function in which I am trying to get a target found:

     void Update () {
         target = null;
         if (this.tag == "User")
         {
             
            target = FindClosestTarget.findclosesttarget("Enemy", this.transform);
             move();
         }
         if (this.tag == "Enemy")
         {
             target = FindClosestTarget.findclosesttarget("User", this.transform);
             move();
         }
     }


The Good news is that every time, a target is returned.

The PROBLEM is that when I have this script running on multiple units, they all return the SAME target of their respective designation. (ex All guys with "User" focus on ONE target with "Enemy" at all times regardless of other conditions)

My hunch is that I am supposed to do something along the lines of like "FindClosestTarget targ = new FindClosestTarget" or something within that area, but I haven't had much luck in that regards.

Any help is greatly appreciated.

***UPDATE

I've isolated the problem somewhere in this area on script 1

 {
                 GameObject[] gos = GameObject.FindGameObjectsWithTag(a);

                 foreach (GameObject go in gos)
                 {
                     Vector3 diff = go.transform.position - position;
                     float curDistance = diff.sqrMagnitude;
                     if (curDistance < distance)
                     {
                         if (go.GetComponent<Life>() != null)
                         {
                             distance = curDistance;
                             return go;
     
                         }
 }
 }
 }

As gos seems to only be grabbing 1 object rather than an array of the entire screen

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
0

Answer by tag104367 · Oct 30, 2016 at 09:02 PM

So you won't need to call a new FindClosestTarget() anywhere seeing how your function is static doing new won't do anything. Not sure if this is the complete solution but it looks like you are passing in a transform to the function using this.transform (lines 6, 11 of second script) and then you are using the thisobject.transform.position in your first script. Maybe try using thisobject.position instead of thisobject.transform.position

Comment
Add comment · Show 1 · 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 bishop87 · Oct 31, 2016 at 10:23 AM 0
Share

I changed thisobject.transform.position, unfortunately it still yields the same results in that everyone still pools to one target :\

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

When to use a namespace? 2 Answers

Monobehaviour Update not working in namespace 2 Answers

How do I call a variable from another script? (C#) 1 Answer

Unity namespace cannot be found (C# MonoDevelop) 1 Answer

Valve.VR or Valve.VR.InteractionSystem not found 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