Accessing a (complex) GameObject from a script attached to the GameObject

Hey,

I am making a few changes to the UMA package of Fernando and there is a gameObject in the Unity Editor example scene called UMA. Well, I want to access a script called UMACrowd and cause it to create a new UMA character by setting a Boolean called generateUMA to true from another script attached to the UMA gameObject called UMA Customization.

This is Unity 9 to 5 so how do I do this?

The class I want to access is attached as a script to the ‘UMA’ GameObject and has a Boolean called generateUMA in a class called UMACrowd also attached to the UMA GameObject. I try to do this in the UMAWindow function at the bottom of the UMACustomization class.

+++++

OK, so I’ve changed a small snippet of code in the UMAWindow function in the UMACustomization class:

               if (GUILayout.Button(sexArray[0].itemName, GUILayout.Width(75)))
			{
			
			GameObject uma   = GameObject.Find("UMA") as GameObject;
			UMACrowd umaChar = uma.GetComponent("UMACrowd.cs") as UMACrowd;
			
			umaChar.generateUMA = true;
			}// if
		
		if (GUILayout.Button(sexArray[1].itemName, GUILayout.Width(75)))
			{
			
			GameObject   uma = GameObject.Find("UMA") as GameObject;
			UMACrowd umaChar = uma.GetComponent("UMACrowd.cs") as UMACrowd;
			
			umaChar.generateUMA = true;
			}// if

It doesn’t like this:

NullReferenceException: Object reference not set to an instance of an object
UMACustomization.UMAWindow (Int32 windowID) (at Assets/UMA/UMA_Project/Scripts/UMACustomization.cs:685)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/ea95e74f6e5f192d/Runtime/ExportGenerated/Editor/GUI.cs:1324)

“UMA” is the empty GameObject created in the Unity Editor (see attached image) that have the following attached to it:

a. OverlayLibrary
b. SlotLibrary
c. RaceLibrary
d. UMACrowd
e. UMACustomization
f. UMAGenerator

I get this:

Assets/UMA/UMA_Project/Scripts/UMACustomization.cs(682,64): error CS0039: Cannot convert type UnityEngine.Component' to UnityEngine.GameObject’ via a built-in conversion

Is “UMA” do high a level object to retrieve this way?

Thanks.


1402636–72866–$UMACustomization.cs (23.4 KB)

Crud, only 25 views…guess I’ll have to sit through some tutorial videos. :frowning:

Or maybe it’s harder than I thought.

It looks like your syntax may be wrong. Try replacing UMACrowd umaChar = uma.GetComponent("UMACrowd.cs") as UMACrowd; with

UMACrowd umaChar = uma.GetComponent<UMACrowd>();

Also, converting GameObject to GameObject is unnecessary.GameObject.Find("UMA"); should suffice.

Best regards,
Peter.

Thanks:

        if (GUILayout.Button(sexArray[0].itemName, GUILayout.Width(75)))
			{
			
			GameObject uma   = GameObject.Find("UMA");
			UMACrowd umaChar = uma.GetComponent<UMACrowd>();
			
			umaChar.generateUMA = true;
			}// if
		
		if (GUILayout.Button(sexArray[1].itemName, GUILayout.Width(75)))
			{
			
			GameObject uma   = GameObject.Find("UMA");
			UMACrowd umaChar = uma.GetComponent<UMACrowd>();
			
			umaChar.generateUMA = true;
			}// if
NullReferenceException: Object reference not set to an instance of an object
UMACustomization.UMAWindow (Int32 windowID) (at Assets/UMA/UMA_Project/Scripts/UMACustomization.cs:685)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/ea95e74f6e5f192d/Runtime/ExportGenerated/Editor/GUI.cs:1324)

Apparently the Find statement works (GameObject uma = GameObject.Find(“UMA”):wink: because the uma.GetComponent doesn’t complain of a null reference (UMACrowd umaChar = uma.GetComponent():wink: but uma.GetComponent fails (umaChar.generateUMA = true;) causes a Null Reference…

OK, so the reason I’m trying to set the generateUMA Boolean is because when it’s set in the UMACrowd script the Update function called GenerateOneUMA() and a procedure character is generated.

the generateUMA Boolean is public.

I really don’t understand why the GetComponent isn’t working. I’ve tried all 3 or 4 legal variants of that method.

I’ve attached the UMACrowd code if that makes it easier.

In the Unity Editor if I check mark the edit script variable on the UMACrowd class ‘Generate UMA’ a character is generated when I press play.

I’ve done similar before I’m perplexed.

1403347–72937–$UMACrowd.cs (18.8 KB)

Thanks everyone.

I’ve learned that UMA and the attached scripts of classes in the Unity Editor are seen as a GameObject class not as a gameObject instance when I press ‘Play’. That seems weird to me, like interpreting a script rather than running an new object but changing the bool generateUMA to static in UMACrowd made the change easy:

new:
public static float atlasResolutionScale;
public static bool generateUMA;
public static bool generateLotsUMA;
public static Vector2 umaCrowdSize;
public static bool randomDna;

old:
public float atlasResolutionScale;
public bool generateUMA;
public bool generateLotsUMA;
public Vector2 umaCrowdSize;
public bool randomDna;

It looks like the lazy man’s way of doing this will be to redefine the proper variables in UMACrowd as static so they can be set in UMACustomization via onGUI / Unity GUI.

I want to minimize code changes to make it quicker to update future versions of UMA with these changes.

Ciao