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 /
avatar image
0
Question by mats1105 · Feb 16, 2019 at 02:17 PM · return valuereturn type

Returning new class from a method not working

Hi,

I am trying to run a method that returns a class with an int and a bool. Problem is that no matter what int and bool value the method returns it always is received as 0 and false.


Here is my method that runs the operation:

 public void CheckSomething(something)
 {
     ReturnClass returnClass = otherScript.CheckForItem(something);
 
     Debug.Log(returnClass.exists); //prints false to the console
     Debug.Log(returnClass.index); //prints 0 to the console
 
     if (returnClass.exists)
     {
         DoSomething(returnClass.index, something); //this should run, but does not because returnClass.exists == false
     }
 }

And here is the method that returns the class:

(I simplified it to always return the same values for testing purposes)

 public ReturnClass CheckForItem(string item)
 {
     //checking if the item exists and the index of the item
 
     return new ReturnClass(true, 3);
 }

And here is the class I am returning:

 public class ReturnClass
 {
     public int index;
     public bool exists;
 
     public ReturnClass(bool foundItem, int indexOfItem)
     {
         int index = indexOfItem;
         bool exists = foundItem;
     }
 }



Am I doing something wrong?

Thanks in advance for any answers :)

Comment
Add comment · Show 2
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 zereda-games · Feb 16, 2019 at 03:55 PM 0
Share

$$anonymous$$ay just be a typo but why do you have public void CheckSomething(something)

and not

public void CheckSomething(string something)?

Also,

 public ReturnClass CheckForItem(string item)//<- string isn't doing anything..
 {
     //checking if the item exists and the index of the item
     return new ReturnClass(true, 3);
 }

you should have something more like

 public ReturnClass CheckForItem(int itemIndex)
 {
      //checking if the item exists and the index of the item
      return new ReturnClass(true, itemIndex);
 }

or

 public Bool CheckForItem(string something,int itemIndex)
 {
     //checking if the item exists and the index of the item
     if(Equals(ReturnClass.items[itemIndex],itemIndex)){
        Debug.Log(something);
        return Equals(ReturnClass.Items[itemIndex], itemIndex);
     } else {
        return Equals(ReturnClass.Items[itemIndex], 0);
     }
 }

Further more you could add each new int and bool to a list like so:

 public class ReturnClass
 {
   public static List<int> indexs;
   public static List<boo>l exists;
  
   public ReturnClass(bool foundItem, int indexOfItem)
   {
       if(!Equals(indexs.Contains(indexOfItem),true){
            indexs.Add(indexOfItem);
       } else {
            Debug.Log("List already contains the value.")
       }
       if(!Equals(indexs.Contains(indexOfItem),true){
            exists.Add(foundItem);
       } else {
            Debug.Log("List already contains the value.")
       }
   }
 }


now you can call the class directly for the variables:

  public void CheckSomething(int index, string something)
  {
      if(Equals(ReturnClass.indexs,null)){
          ReturnClass.indexs.Add(0);
      }

      bool checker= otherScript.CheckForItem(something, index);
      //[Edit]Added
      if(!Equals(checker,true)){
          return;
      }

      for(int i=0;i<ReturnClass.indexs.Count;i++){
         Debug.Log(ReturnClass.exists[i]); //prints false to the console
         Debug.Log(ReturnClass.indexs[i]); //prints 0 to the console
         if (returnClass.exists[i])
         {
          DoSomething(returnClass.index, something);
         }
      }
  }



avatar image zereda-games zereda-games · Feb 16, 2019 at 04:34 PM 0
Share

also forgot a way to create new ReturnClass's to add to those lists.. enough coding for me you figure it out [Edit] I'm also not using the bool check anywhere after setting it. just thought you should know -> Corrected

1 Reply

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

Answer by RobAnthem · Feb 16, 2019 at 03:38 PM

Notice how in this class you define "int index" and "bool exists" inside the constructor?

  public class ReturnClass
  {
      public int index;
      public bool exists;
  
      public ReturnClass(bool foundItem, int indexOfItem)
      {
          int index = indexOfItem;
          bool exists = foundItem;
      }
  }

By defining the type of variable or field, you actually allocate ram to a new local field of that type, IE, you're parameters are never actually being passed to the fields of the class. You need to use THIS keyword in front of the fields or reference the fields by default, like this.

  public class ReturnClass
  {
      public int index;
      public bool exists;
  
      public ReturnClass(bool foundItem, int indexOfItem)
      {
          index = indexOfItem;
          exists = foundItem;
      }
  }
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 Bunny83 · Feb 16, 2019 at 03:54 PM 1
Share

Right. I've converted your comment into an answer since this is essentially the issue. Note that it would be highly recommended to use a struct for this kind of information ins$$anonymous$$d of a class. Though a common C# technique is to use an out parameter. So "CheckForItem" could look just like this:

 public ReturnClass CheckForItem(string item, out int index)
 {
     index = -1;
     //checking if the item exists and the index of the item
 
     index = 3;
     return true;
 }

To use this the common pattern looks like this:

 public void CheckSomething(something)
 {
     int index;
     if (otherScript.CheckForItem(something, out index))
     {
              DoSomething(index, something);
     }
 }


Though when working with indices it's actually simpler to just return an invalid index to indicate that no item was found. Commonly "-1" is used since indices almost always start at 0. For example this is used by string.IndexOf.

avatar image zereda-games Bunny83 · Feb 16, 2019 at 04:55 PM 0
Share

@Bunny83 You always seem to have decent answers, in my post I'd rather ask your opinion, am i wrong? i know i'm missing something but, this isn;t my code and i don;t wanna work on it LOL.

avatar image mats1105 Bunny83 · Feb 16, 2019 at 06:32 PM 0
Share

This seems interesting, thanks for explaining :)

avatar image mats1105 · Feb 16, 2019 at 06:31 PM 0
Share

Thanks, That was a silly mistake on my part.

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

101 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving Coroutines To One Method 1 Answer

How do I display the final score? 1 Answer

How to return value from UnityWebRequest? 0 Answers

Return value from coroutine 2 Answers

member names cannot be the same as their enclosing type 2 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