Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Having problems passing structure back from a method

Discussion in 'Scripting' started by SoCentral2, Oct 27, 2015.

  1. SoCentral2

    SoCentral2

    Joined:
    Oct 6, 2015
    Posts:
    10
    I've created the following simplified scenario to illustrate my problem.

    I created a new project. I created 2 empty gameobjects and made one the child of the other. I called them MyParentGameObject and MyChildGameObject.

    I created a script called MyChild and attached it to MyChildGameObject. I created a script called MyParent and attached it to MyParentGameObject.

    Here are the contents of MyChild script :-

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6. public class MyChild : MonoBehaviour {
    7.  
    8.     public struct MyStructureInChild
    9.     {
    10.         public int ReturnedInteger;
    11.         public string ReturnedStringA;
    12.         public bool ReturnedBool;
    13.         public string ReturnedStringB;
    14.     }
    15.     public MyStructureInChild myStructureInChild;
    16.  
    17.     public MyStructureInChild DoSomething(string PassedIn)
    18.     {
    19.         myStructureInChild.ReturnedInteger = 101;
    20.         myStructureInChild.ReturnedStringA = "Hello";
    21.         myStructureInChild.ReturnedBool = true;
    22.         myStructureInChild.ReturnedStringB = "World";
    23.         return myStructureInChild;
    24.     }
    25.  
    26. }
    Here are the contents of MyParent script : -

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyParent : MonoBehaviour {
    5.  
    6.     public struct MyStructureInParent
    7.     {
    8.         public int ReturnedInteger;
    9.         public string ReturnedStringA;
    10.         public bool ReturnedBool;
    11.         public string ReturnedStringB;
    12.     }
    13.     public MyStructureInParent myStructureInParent;
    14.  
    15.     void Awake()
    16.     {
    17.         myStructureInParent = transform.Find("MyChildGameObject").GetComponent<MyChild>().DoSomething("Hello");
    18.     }
    19. }
    The problem is that I get the following error from Line 17 of MyChild "Cannot implicitly convert type 'MyChild.MyStructureInChild' to 'MyParent.MyStructureInParent' ".

    error.png

    Since the structures are identical, I would expect there to be no problem.
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    If I'm not mistaken, even though two structs appear identical, your compiler doesn't see it that way. For compiler it is the same as trying to convert class A into B that are not the same (don't share same parent class).

    If you know your structs will have same values, you could define a single global struct:
    Code (CSharp):
    1. public struct MyGlobalStruct
    2. {
    3.      public int ReturnedInteger;
    4.      public string ReturnedStringA;
    5.      public bool ReturnedBool;
    6.      public string ReturnedStringB;
    7.  }
    8.  
    9. public class MyClass : MonoBehaviour
    10. {
    11.     // rest of class...
    12. }
    13.  
    Hope it helps. If I'm mistaken, please correct me!
     
    SoCentral2 and CrymX like this.
  3. SoCentral2

    SoCentral2

    Joined:
    Oct 6, 2015
    Posts:
    10
    Thanks for that. I'm going down the path of a global struct, I was just wondering why this problem occurs.