- Home /
Lost reference to gameObject when passing it as argument
Hello,
I'm facing a problem with Unity losing reference to gameObject that is passed into the function parameters. This is my code:
public void RequestRenderersEnable(GameObject transparent, bool value)
{
// Give authority to edit transparent logo child objects
DebugTest((transparent == null) + " " ); // Outputs "False"
Renderer[] meshRenderers = transparent.GetComponentsInChildren<MeshRenderer>();
DebugTest(meshRenderers.Length + " "); // No problem
foreach (Renderer r in meshRenderers)
{
DebugTest(r.gameObject.name); // Still ok, outputs name of the first gameObject from renderers gameObjects
CmdServerAssignClient(r.gameObject);
}
...
}
[Command]
void CmdServerAssignClient(GameObject objectInHand)
{
NetworkIdentity targetObjectNetID;
DebugTest((objectInHand == null) + ""); // Outputs True
DebugTest(objectInHand.name); // Null reference exception
...
}
// Just some debug functions
public void DebugTest(string message)
{
CmdDebug(message);
}
[Command]
void CmdDebug(string message)
{
Debug.Log(message);
}
However, if I create empty GameObject inside RequestRenderersEnable() and pass it to CmdServerAssignClient, reference is not lost. Is this some Unity bug? The "transparent" GameObject parameter is originally obtained from another GameObject's method by calling:
thisPlayer.GetComponent<RequestObjectControl>().RequestRenderersEnable(transparent, true);
where "transparent" is assigned publicly in Inspector, and is not used until this line.
What might be the issue? I've never come across problem like this in my life. Thank you for response.
Answer by JayMZee · Aug 10, 2017 at 11:01 AM
Oh well, I'm dumb... I forgot to add NetworkIdentity to "transparent" child objects... That solved the problem.
Your answer
Follow this Question
Related Questions
Show Hosted LAN Matches 0 Answers
Reference to local player on multiplayer? 2 Answers