- Home /
Why is not assigning new value ?
[HideInInspector]
public Vector3 blueScale;
[HideInInspector]
public Vector3 orangeScale;
[HideInInspector]
public Vector3 yellowScale;
[HideInInspector]
public Vector3 purpleScale;
[HideInInspector]
public Vector3 greenScale;
[HideInInspector]
public Vector3 redScale;
public float blueEnergy;
public float greeEnergy;
public float orangeEnergy;
public float purpleEnergy;
public float redEnergy;
public float yellowEnergy;
[HideInInspector]
public Transform blue;
[HideInInspector]
public Transform green;
[HideInInspector]
public Transform orange;
[HideInInspector]
public Transform purple;
[HideInInspector]
public Transform red;
[HideInInspector]
public Transform yellow;
public GameObject _blue;
public GameObject _green;
public GameObject _orange;
public GameObject _purple;
public GameObject _red;
public GameObject _yellow;
public Transform FindChild( GameObject parent, string name)
{
if (parent.name == name)
{
Debug.Log("Parent's Name is same as you are searching for");
return null;
}
Transform pTransform = parent.GetComponent<Transform> ();
foreach (Transform t in pTransform)
{
if (t.name == name)
{
return t;
}
}
return null;
}
public void WhenYouDoSomething(GameObject item, Transform itemTransforms, Vector3 itemScale)
{
Debug.Log ("This is when");
if (item == null)
Debug.Log ("Item is null");
else
{
itemTransforms = FindChild ( item, "line");
itemScale = itemTransforms.localScale;
}
Debug.Log (itemTransforms);
}
public void FindItemsObjects()
{
_blue = GameObject.FindWithTag ("Blue");
Debug.Log (_blue);
WhenYouDoSomething (_blue, blue, blueScale);
Debug.Log (blue);
_green = GameObject.FindWithTag ("Green");
WhenYouDoSomething (_green, green, greenScale);
_orange = GameObject.FindWithTag ("Orange");
WhenYouDoSomething (_orange, orange, orangeScale);
_purple = GameObject.FindWithTag ("Purple");
WhenYouDoSomething (_purple, purple, purpleScale);
_red = GameObject.FindWithTag ("Red");
WhenYouDoSomething (_red, red, redScale);
_yellow = GameObject.FindWithTag ("Yellow");
WhenYouDoSomething (_yellow, yellow, yellowScale);
}
void Start()
{
blue = null;
green = null;
orange = null;
purple = null;
red = null;
yellow = null;
FindItemsObjects ();
}
When I debug.
Debug.Log (blue); it gives me null, but in WhenYouDoSomething function when I debug Debug.Log (itemTransforms); it gives me the object with name line.
Why that transform is not assigned to blue ? or anyotherTransform that I declared?
Answer by Chelmney_ · Aug 12, 2014 at 01:42 PM
Hello,
try changing the function declaration to this:
public void WhenYouDoSomething(GameObject item, out Transform itemTransforms, Vector3 itemScale)
Similarly, you will have to call the function like this:
WhenYouDoSomething (_blue, out blue, blueScale);
This way you're passing the variable to the function by reference and not by value.
The out parameter `itemTransform' must be assigned to before control leaves the current method
this is the error Im getting
k, changing from out to ref, solved the problem THAN$$anonymous$$ YOU!
Add itemTransforms = null in the first if branch like this:
if (item == null)
{
Debug.Log ("Item is null");
itemTransforms = null;
}
Variables with out parameters must always be assigned a value in a function and in your version this was not the case because you only assigned it a value in the else branch.
Your answer
Follow this Question
Related Questions
New to Unity, Need Help. 1 Answer
Player lives script help 1 Answer
C# -- Build character unit from script 1 Answer
C# Randomly Adding Elements from stringListA to stringListB 1 Answer
C# Input.GetKey("Tab") Double Tap 1 Answer