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 greg1992 · Mar 20, 2013 at 10:11 AM · c#

Turrets; Error CS0161 C#

Hiya all, making my own turret system as we speak however i'm getting a rather annoying error, i know part of my code is contradicting something.

"Assets/Standard Assets/New Folder/TurretScript.cs(62,21): error CS0161: `TurretScript.FindClosestEnemy()': not all code paths return a value"

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class TurretScript : MonoBehaviour {
 
     
 
     
 
     
 public Transform Bullet;
     
 public Transform Enemy;
     
 
 public int cost;
 
 public float Radius;
     
     
         
         
         
    
     
     
 
 
 
  
     
 
  
 
     // Use this for initialization
 
     void Start () {
         
     
 
     }
 
     
 
     // Update is called once per frame
 
     void Update () {
     
 }
     
     void OnTriggerEnter(Collider collider)
     {
         if(collider.gameObject.tag=="Enemy")
         {
             Debug.Log ("EnemyIsWorking");
         FindClosestEnemy();
         }
     }
     
      GameObject FindClosestEnemy() {
         GameObject[] gos;
         gos = GameObject.FindGameObjectsWithTag("Enemy");
         GameObject closest = null;
         float distance = Mathf.Infinity;
         Vector3 position = transform.position;
         foreach (GameObject go in gos) {
             Vector3 diff = go.transform.position - position;
             float curDistance = diff.sqrMagnitude;
             if (curDistance < distance) {
                 closest = go;
                 distance = curDistance;
             }
         }
        LookAtTarget();
     }
     
     
     void LookAtTarget()
     {
     Transform.rotation = Quaternion.identity;
         
     }
     
     
     
 }
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
Best Answer

Answer by GuyTidhar · Mar 20, 2013 at 10:28 AM

Add:

 return closest;

After calling: 'LookAtTarget();' inside FindClosestEnemy();

You have wrote a function that returns a value - it must do so.

Note:

Looking at your code it looks as if you don't need that return value since you do not set it.

If that is the case, simply change 'GameObject' to 'void' in the return type definition at the function name line.

 void FindClosestEnemy()

instead of

 GameObject FindClosestEnemy()

This way, you don't need to return the value (and ditch my first suggestion).

Comment
Add comment · Show 4 · 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 greg1992 · Mar 20, 2013 at 10:53 AM 0
Share

Okay thankyou it gets rid of the error anyway.

Should i return the value? i'm wanting my turret to rotate and face the spawned enemies and shoot at the closest one, and then change to face the next closest enemy once the previous has left the turrets radius.

avatar image GuyTidhar · Mar 20, 2013 at 11:04 AM 0
Share

If you need the value outside of the scope of your function - then do return it.

If not - there is no need to.

avatar image greg1992 · Mar 20, 2013 at 11:08 AM 0
Share

Okay brilliant, thanks for the help man, have a good day.

avatar image GuyTidhar · Mar 20, 2013 at 11:13 AM 0
Share

Sure thing buddy.

Glad to have helped.

And generally speaking please make it a habit to mark correct answers as such using the UI. This clarifies the forum for others when they search for existing issues and serves as a friendly courtesy for those who helped you.

Cheers!

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Not all code paths return a value? 2 Answers

bza, Saving color to player prefs 2 Answers

Flip over an object (smooth transition) 3 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