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. Dismiss Notice

Passing a struct from one object to another

Discussion in 'Scripting' started by ericcowan, Nov 7, 2020.

  1. ericcowan

    ericcowan

    Joined:
    Oct 24, 2013
    Posts:
    15
    Thanks for looking. I have a script that has a struct and data settings and I'm trying to pass the settings to another script on another object.

    I've read that I need to refer to the original script for the struct, so I'm trying just that. Once the variable is created, I should be able to call the GetHoverOverSettings() method to populate the fields.

    First Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SoldierScript : MonoBehaviour
    6. {
    7.  
    8.     public struct HoverOverSettings
    9.     {
    10.         public int hoverOverSegments;
    11.         public float lineThickness;
    12.         public float xRadius;
    13.         public float zRadius;
    14.         public float yHeight;
    15.     };
    16.  
    17.     public HoverOverSettings hoverOverSettings = new HoverOverSettings();
    18.  
    19.     void Start()
    20.     {
    21.         SethoverOverSettings();
    22.  
    23.     }
    24.  
    25.     public void SethoverOverSettings()
    26.     {
    27.  
    28.         hoverOverSettings.hoverOverSegments = 30;
    29.         hoverOverSettings.lineThickness = 0.2f;
    30.         hoverOverSettings.xRadius = 0.2f;
    31.         hoverOverSettings.zRadius = 0.2f;
    32.         hoverOverSettings.yHeight = 0.3f;
    33.     }
    34.  
    35.     public HoverOverSettings GetHoverOverSettings()
    36.     {
    37.  
    38.         return hoverOverSettings;
    39.     }
    40. }
    The second and the GameObject had
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //[RequireComponent(typeof(LineRenderer))]
    5. public class LineR : MonoBehaviour
    6. {
    7.     public GameObject parentObject;
    8.  
    9.     void Start()
    10.     {
    11.         parentObject.GetComponent<SoldierScript>.HoverOverSettings hoverOverSettings = new parentObject.GetComponent<SoldierScript>.HoverOverSettings();
    12.     }
    13. }
    the first script in it:
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Code (CSharp):
    1. parentObject.GetComponent<SoldierScript>().hoverOverSettings = new SoldierScript.HoverOverSettings();
     
  3. ericcowan

    ericcowan

    Joined:
    Oct 24, 2013
    Posts:
    15
    Thank you!
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    Sorry but this line makes no sense ^^
    It seems you have trouble understanding the difference between a class / type and an instance of that class / type. Your "HoverOverSettings" struct is a nested type of your "SoldierScript" class. You don't need any instance of your SoldierScript in order to use the nested type. To declare a local variable of your struct type you just use

    SoldierScript.HoverOverSettings yourVariableName;


    Note that you can't really pass an instance of a struct around. Well it's possible when you have a method with a ref parameter. In all other cases structs are always copied. I'm not sure what the line I quoted should actually do. It looks like you just create a new empty struct instance locally. If you want to copy the struct from the SoldierScript instance you would do

    Code (CSharp):
    1. SoldierScript.HoverOverSettings hoverOverSettings =parentObject.GetComponent<SoldierScript>.GetHoverOverSettings();
    or shorter with a local var:

    Code (CSharp):
    1. var hoverOverSettings = parentObject.GetComponent<SoldierScript>.GetHoverOverSettings();
    Keep in mind that this variable will be a copy of the instance in your SoldierScript.
     
    ericcowan and Vryken like this.
  5. ericcowan

    ericcowan

    Joined:
    Oct 24, 2013
    Posts:
    15
    Now that did work. How come I didn't have to refer to the SoldierScript.HoverOverSettings with a parentObject.GetComponent? I thought I would have to point to it.

    THANKS!

    Code (CSharp):
    1.     void Start()
    2.     {
    3.  
    4.         SoldierScript.HoverOverSettings hoverOverSettings;
    5.  
    6.         hoverOverSettings = parentObject.GetComponent<SoldierScript>().GetHoverOverSettings();
    7.  
    8.         Debug.Log(hoverOverSettings.xRadius);
    9.  
    10.     }