- Home /
3D Platformer Tutorial getting Lerpz's SkinnedMeshRenderer using c#
Okay, first this tutorial has taught me a lot about sinking my teeth into Unity. My goal as a learning exercise was to convert all the scripts from javascript to C# to improve structured coding. And after spending most of the day working through it, I still have a problem with one particular issue -- Hiding Lerpz when he dies and before he respawns.
In my hierarchy Lerpz as the following structure:
> Player
> blob shadow projector
> Lerpz
> rootJoint
> hipLeft
> hipRight
> torso
ThirdPersonController.js has the following original HidePlayer function:
function HidePlayer()
{
GameObject.Find("rootJoint").GetComponent(SkinnedMeshRenderer).enabled = false;
isControllable = false;
}
I converted it to C# as follows:
void HidePlayer()
{
GameObject.Find("rootJoint").GetComponent<SkinnedMeshRenderer>().enabled = false;
isControllable = false;
}
However the JS one works fine and the C# version nails me with this exception:
NullReferenceException: Object reference not set to an instance of an object
After debugging it, and looking at the hierarchy, the Lerpz object contains the SkinnedMeshRenderer, not rootJoint. So as a test, I wrote this workaround as a test:
GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<SkinnedMeshRenderer>().enabled = false;
So can anyone explain the difference in behavior between JS and C#? Also what is the proper C# conversion for that function? Thanks!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't load Mouse Cursor texture 1 Answer
RaycastHit2D not interacting with tags 1 Answer
NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer