Get game component type?

I have the following code in C#:

		GameObject[] obj = GameObject.FindGameObjectsWithTag("Player");
	
		foreach(GameObject o in obj)
		{
			o.transform.position = new Vector3(o.transform.position.x, o.transform.position.y + 5, 
			                                   o.transform.position.z);
		
			Health h = o.GetComponent<Health>();
		}

It all works fine save for the line Health h = o.GetComponent(); and the reason for this is the type Health is undefined.

Health is a js script which can be accessed by js functions simply as ‘Health’, but as far as I can see there is no way to find it in C#.

Is this just a limitation of Unity’s scripting system (which would be understandable) or can this be done and I’m just going about it the wrong way?

1 Answer

1

That happens because of compile order. If you want your c# and JS scripts to talk nicely to each other you’ll have to put the JS scripts under Assets/Plugins folder (possibly also Resources folder, but I haven’t confirmed that yet).

Using both languages seems like more trouble than it’s worth it. I’d advise to stick to one of them whenever possible.