- Home /
Null Reference Excepting :Object reference not set to an instance of object while changing text of 3D text
Hey guys i have this prefab structure .. Prefab 1. Sphere 2. Clyinder 3. Sphere 4. 3D text... Now i instantiate the prefab at runtime from this code :
GameObject go = (GameObject)Instantiate(Resources.Load("Prefab1"));
After instantiating the prefab i want to change the text of 3D text depending on click of which element it is instantiated. And this is what i have tried as suggested by many post out here.But its giving me the Null reference exception.
go.GetComponent().text=symbol;
Can anyone suggest what the problem is there. ?
Answer by sanks007 · Jul 02, 2013 at 05:39 AM
go.GetComponentInChildren().text=symbol;
With this its working completely fine. and yeah the nullreferenceexception was may be the font was missing in the inspector.Why unity dont let me specify < TextMesh > before the "()".I tried it many a times but it doesn't.
Answer by Graham-Dunnett · Jul 01, 2013 at 11:26 AM
GetComponent
usually takes an argument which is the name of the component you are trying to access. In your case that's probably the TextMesh
component.
I am passing < Text$$anonymous$$esh > as argument to GetComponent().
checked this link as well http://forum.unity3d.com/threads/9843-Accessing-Text$$anonymous$$esh-text-inside-a-instance-of-a-prefab but no use again it gives me NullReferenceException