- Home /
Question by
BAD-TIGERS-GAMES · Jun 30, 2015 at 04:27 PM ·
constructors
constructor return value
how i can make constructor more simple and eficient i need function return (GO1,txt1,GO1Vector)if reset direction true, (GO2,txt2,GO2Vector)if reset direction false
using UnityEngine;
using System.Collections;
public class answer : MonoBehaviour {
public GameObject GO1;
public GameObject GO2;
public string txt1;
public string txt2;
public Vector3 GO1Vector;
public Vector3 GO2Vector;
public bool resetDirection;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
resetDirection = !resetDirection;
}
if(Input.GetKeyDown(KeyCode.U))
{
if(resetDirection)Reset(GO1,txt1,GO1Vector);
if(!resetDirection)Reset(GO2,txt2,GO2Vector);
}
}
void Reset(GameObject GO, string txt, Vector3 GOVector)
{
GO.name = "1";
txt = "2";
GOVector = new Vector3(0f,0f,0f);
if(resetDirection)
{
GO1.name =GO.name;
txt1 = txt;
GO1Vector = GOVector;
}
else
{
GO2.name =GO.name;
txt2 = txt;
GO2Vector = GOVector;
}
}
}
Comment
Best Answer
Answer by DiegoSLTS · Jun 30, 2015 at 05:01 PM
First, that's not a constructor.
Second, you can't return multiple objects in C# unless the function returns some array or list of generic types (a really bad approach).
Third, your Reset function doesn't return anything.
One option to "return" multiple objects is to use the "out" keyword on the function parameters: https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx
very useful , thancks alot , i understand and make it
Your answer
