How to spawn GameObject exactly in front of another object?

Im working on a 3D game in Unity and I’m having a problem trying to spawn an object exactly in front of a wall in my game, like hanging a picture on it. I don’t want the object to look like it’s too far from the wall or overlapping the wall.

public void SpawnPrefab(Transform prefab)
{
    compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
    Vector3 mousePosition, targetPosition;
    InputManager inputManager;
    inputManager = prefab.gameObject.GetComponent<InputManager>();
    mousePosition = Input.mousePosition;
    targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,mousePosition.z + compSize/2 + wallSize/2));
    prefab.localPosition = targetPosition;
    if(isAlreadyClicked == false)
    {
        GameObject comp = (GameObject) Instantiate(prefab.gameObject, new Vector3(prefab.transform.position.x, prefab.transform.position.y, prefab.transform.position.z + compSize/2 + wallSize /2), prefab.transform.rotation);
        isAlreadyClicked = true;
    }
}

I tried adding float value on the mouseposition.z like mouseposition.z + but it doesn’t seem working like how I wanted because I have different object with different sizes, where some objects spawn at the back of the wall, while some will spawn too far at the front from the wall.

Any help would be appreciated.

Thanks thanks thanks!

You are going to have to take your ScreenToWorldPosition() result and then run a RayCast collision trace out from that point forward and place your object at the resulting hit position minus the bounds of the object. Sounds like you will also want to use the resulting surface normal to define the rotation of the object so it aligns to it.

You can add an Empty GameObject and place it exactly where you want to spawn the objectc, we do this to keep track of the transform.
In your script you add

   public Transform emptyObjectTransform;

to get hold of the values, you will need to drag and drop the empty object to the script in the inspector,
no when you pass the spawn values in your “Instantiate” method you pass

emptyObjectTransform.position

You can also pass rotation…

Dont recomend this method if you are going to spawn many items because you will need one emptygame object for each spawn pont

Thanks for the replies, but I managed to get it done. Here’s the full code

public void SpawnPrefab(Transform prefab)
	{
		InputManager inputManager;
		clicked = false;
		compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
		inputManager = prefab.gameObject.GetComponent<InputManager>();
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if(Physics.Raycast(ray, out hit))
		{
			Debug.Log("clicked");
			if(isAlreadyClicked == false)
			{
				OnClickedButton();
				Instantiate(prefab, new Vector3(hit.point.x, hit.point.y, frontWall.gameObject.transform.position.z + compSize*2 - wallSize*3), prefab.transform.rotation);
				isAlreadyClicked = true;
				Debug.Log(clicked);
			}
		}
	}

This is much more simpler solutions than what I did before.
Source: this question , and this Unity question