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

How to return multiple time at the same time

Discussion in 'Scripting' started by justadeveloper, Apr 4, 2015.

  1. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33
    Hello guys,
    So I want to expose my function to another script, and I want to expose a function that has return multiple values.I want to know how to send that value to another script at the same time?

    Public int sendTriggerStatus()
    {
    //return trigger1,trigger2,trigger3;
    }
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Let's solve one problem after another. First, if you only want to return a bunch of objects of the same type, you can use an array or a list to bundle them and then return the array or list. It is not possible to return more than one object or value in .net at the same time.

    The code to return three triggers at the same time may look as follows:
    Code (csharp):
    1. public int SendTriggerStatus()
    2. {
    3.     Collider[] triggers = new Collider[3] {trigger1, trigger2, trigger3};
    4.     return triggers;
    5. }
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Also if not everything is the same data type you could also create a struct and fill it with data and return that.
     
  4. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33
    I want to send bool variable to other script, so i want to access the function sendTriggerStatus variables,trigger1,trigger2,and trigger3 in other script,if the variables are true then ill setactive the other gameobjects based on trigger status

    Public int sendTriggerStatus()
    {
    //return trigger1,trigger2,trigger3;(trigger1,2,and 3 are bvariables with bool type;
    }

    anyway im using c#
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I think I am starting to understand what your are looking for. It seems more suitable to me to split your functionality up into two different functions. The first one to calculate the trigger status:
    Code (csharp):
    1. public bool TriggerStatus()
    2. {
    3.     return trigger1 && trigger2 && trigger3;
    4. }
    And the second one to activate the other game object. In this code, it is supposed to be in the same script as TriggerStatus, however, this could be changed.
    Code (csharp):
    1. public void SetGameObjectActivity ()
    2. {
    3.     bool status = TriggerStatus ();
    4.     theOtherGameObject.SetActive (status);
    5. }
     
  6. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33
    so this is my codes, actually in CheckTrigger code,I want to make a function that can pass information about trigger1,trigger2,and trigger3 values,and I want to access my trigger1,trigger2,and trigger3 variables in ActiveTheObjects code, but I don't want to make 3 functions to return 3 values,I only want to make 1 function that pass information about each variables.I know that we can separate each of variables into a function that return each value(so there are 3 function if we want to separate the variables and I think it's quite waste to separate each variables in different function)...so how to do this?
     

    Attached Files:

  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you want to access trigger1, trigger2 and trigger3, you can simply make the variable public.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CheckTrigger : MonoBehaviour
    5. {
    6.    public bool trigger1, trigger2, trigger3 = false;
    7.  
    8.    void OnTriggerEnter(Collider other)
    9.    {
    10.      Debug.Log ("Trigger Activated");
    11.  
    12.      if (other.gameObject.tag == "Trigger_Point1")
    13.      {
    14.        trigger1 = true;
    15.      }
    16.    }
    17.  
    18.    public bool TriggerStatus()
    19.    {
    20.      return trigger1 && trigger2 && trigger3;
    21.    }
    22. }
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ActiveTheObjects : MonoBehaviour {
    5.  
    6.    private CheckTrigger setGameObjStatus;
    7.  
    8.    void Start ()
    9.    {
    10.      GameObject GetTriggerStat = GameObject.FindWithTag ("Player");
    11.      if (GetTriggerStat != null)
    12.      {
    13.        setGameObjStatus=GetTriggerStat.GetComponent<CheckTrigger>();
    14.      }
    15.    }
    16.    
    17.    void Update ()
    18.    {
    19.      Debug.Log (setGameObjStatus.trigger1);
    20.      Debug.Log (setGameObjStatus.trigger2);
    21.      Debug.Log (setGameObjStatus.trigger3);
    22.      Debug.Log (setGameObjStatus.TriggerStatus ());
    23.    }
    24. }
     
  8. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33
    Thanks Dude :D