Whats Wrong with this code? (c#)

This code…

using UnityEngine;
using System.Collections;

public class Axisfinder : MonoBehaviour {

	public GameObject xIdentifier;
	public GameObject zIdentifier;
	public int squareType;

	void Start()
	{
		if(GameObject.Find(xIdentifier).GetComponent<AxisTypeSelector>().isRoad == true && GameObject.Find(zIdentifier).GetComponent<AxisTypeSelector>().isRoad == true)
		{
			squareType = 1;
		}
		else if (GameObject.Find(xIdentifier).GetComponent<AxisTypeSelector>().isRoad == true && GameObject.Find(zIdentifier).GetComponent<AxisTypeSelector>().isRoad == false)
		{
			squareType = 2;
		}
		else if (GameObject.Find(xIdentifier).GetComponent<AxisTypeSelector>().isRoad == false && GameObject.Find(zIdentifier).GetComponent<AxisTypeSelector>().isRoad == true)
		{
			squareType = 3;
		}
		else
		{
			squareType = 4;
		}
	}
}

is supposed to allow me to set a gameobject as the X and Y identifier, check those objects for their isRoad status and change the squareType to the number.

however in the inspector there is no option to set a gameobject as the x and y identifiers

it also throws up these errors

Assets/Axisfinder.cs(12,31): error CS1502: The best overloaded method match for `UnityEngine.GameObject.Find(string)’ has some invalid arguments

Assets/Axisfinder.cs(12,31): error CS1503: Argument #1' cannot convert UnityEngine.GameObject’ expression to type `string’

Assets/Axisfinder.cs(16,37): error CS1502: The best overloaded method match for `UnityEngine.GameObject.Find(string)’ has some invalid arguments

Assets/Axisfinder.cs(16,37): error CS1503: Argument #1' cannot convert UnityEngine.GameObject’ expression to type `string’

Assets/Axisfinder.cs(20,37): error CS1502: The best overloaded method match for `UnityEngine.GameObject.Find(string)’ has some invalid arguments

Assets/Axisfinder.cs(20,37): error CS1503: Argument #1' cannot convert UnityEngine.GameObject’ expression to type `string’

I’m fairly new to coding and i’m not sure whats going on

the script its supposed to find isRoad from looks like this.

using UnityEngine;
using System.Collections;

public class AxisTypeSelector : MonoBehaviour {

	public bool isRoad;
	public int roadbuildingSelector;

		void Start()
	{
		roadbuildingSelector = Random.Range (1,3);
		if (roadbuildingSelector == 1)
		{
			isRoad = true;
			Debug.Log("true");
		}
		else
		{
			isRoad = false;
			Debug.Log("false");
		}
	}

}

The error message is pretty self-explanatory once you’ve seen it before. You are calling the GameObject.Find() method and pass it an instance of a GameObject. The Find() method expects a string with the name of the GameObject. A game object instance cannot be converted into a string, hence the error message.

GameObject.Find() is usually used to find an object in the hierarchy that is unknown or non-existing up until runtime. However, your Axisfinder class already has variables to hold instances of the game objects you want to access. So there is no need to find them. So you can omit the Find() method and use something like this instead.

if(xIdentifier.GetComponent<AxisTypeSelector>().isRoad == true && zIdentifier.GetComponent<AxisTypeSelector>().isRoad == true)

Not tested right now, but this is the general gist. So in short, read directly from variables if you can assign them in the inspector. Otherwise, use GameObject.Find() to locate a desired instance at runtime.

Hope that helps.

GameObject.Find() takes a string as parameter, you are putting in a GameObject type.