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 Brutalitywarlord · May 17, 2016 at 03:37 PM · c#collider2dbooleanforeachreturn value

Not all paths return a value

I am making a function that detects if a collision has occurred, found if somewhere and don't understand it much, it appears to return a value, but for some reason the unity editor provides me with a "Not all code paths return a value" error

my code is included below, if anyone can aid me in fixing it or even cleaning it up, i would be most grateful

using UnityEngine; using System.Collections; using UnityEngine.UI; using Random = UnityEngine.Random;

public class Mspawn : MonoBehaviour { public Button button; public BoardManager boardManger; public LayerMask BlockingLayer;

 private float x;
 private float y;
 void OnEnable(){
     
     if (Input.GetMouseButtonDown(0)) {
         button.onClick.AddListener (SpawnUnit);
     }
     
     
 }
 
 void Awake(){
     boardManger = GetComponent<BoardManager> ();
     
 }
 void SpawnUnit()
 {
     
     x = randomSpawnPosition ().x;
     y = randomSpawnPosition ().y;
     //checkposition ();
     for (int i = 0; i < boardManger.PlayerUnits.Length; i++) 
     {
         if (boardManger.PlayerUnits[i].CompareTag("PlayerSoldierMarksman"))
         {
             if (checkposition(randomSpawnPosition()))
             {
                 
             }
             Instantiate (boardManger.PlayerUnits[i], new Vector3 (x,y,0f), Quaternion.identity);
         }
     }
 }
     //This is the problem function, this is where the error occurs
 public bool checkposition(Vector3 SpawnedPosition)
 {
     GameObject[] AllActive = GameObject.FindGameObjectsWithTag ("PlayerHQ"); 
     foreach (GameObject active in AllActive) 
     {
         if (active.transform.position == SpawnedPosition)
         {
             return false;
         }
         else {return true;}
     }
 }
 Vector3 randomSpawnPosition()
 {
     float x = Random.Range(0,2);
     float y = Random.Range(0,2);
     Vector3 Position = new Vector3 (x, y,0f);
     return Position;
     
 }

}

Comment
Add comment · Show 1
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 Owen-Reynolds · May 17, 2016 at 10:17 PM 0
Share

This is a standard C# error -- nothing to do with Unity. Do a Search on that phrase and look in (non-Unity) places for all the explanation you'd ever want.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · May 17, 2016 at 03:41 PM

Didnt read sorry. Based on the title you must make sure that in a function which expects a return value, if you have if else statements or any other kind of branching you must make sure to include a return statement at the end of each branch.

OK I had a little look.

What about the case in which there are 0 GameObjects in AllActive? Both return statements will be skipped.

Comment
Add comment · Show 2 · 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 TreyH · May 17, 2016 at 03:45 PM 0
Share
 public bool checkposition(Vector3 SpawnedPosition)
  {
      GameObject[] AllActive = GameObject.FindGameObjectsWithTag ("PlayerHQ"); 
      foreach (GameObject active in AllActive) 
      {
          if (active.transform.position == SpawnedPosition)
          {
              return false;
          }
          else {return true;}
      }
      // In the event your iterable is empty
      return  true;
  }
avatar image Bonfire-Boy TreyH · May 17, 2016 at 07:58 PM 0
Share

You've fixed it syntactically, but the error in the OP is that the return true is in the wrong place, logically, and it's been left there. This fix, like the OP, only checks the first object in AllActive (it returns from the function regardless of what it finds there). So while it compiles, the logic's still wrong.

Looks to me like the else { return false; } line should be removed. Then it'll check all of them and only return true if none of them is at SpawnPosition.

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

7 People are following this question.

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

Related Questions

NullReferenceException: Object Reference not set to an instance of an object 0 Answers

How to kill enemy when the bullet hits the BoxColllider2D? 1 Answer

How can I make the RyacastHit2D hit an item from far, without my player standing beside it (while blocking my item by its own collider)? 0 Answers

I cant call function another script ! 1 Answer

Best way to detect mouse inside radius of player? 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