Generating an object in a random y position? c#

I am trying to get an object to spawn in a random location on the y axis within a range. My code doesnt show up with any errors until the line “transform.position.y = randomLocation;” and that is the part which needs to be fixed. My code has not been working and I do not know why. would someone mind giving it a look? thanks in advance

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour {

	
	public float minSpawn = 0;
	public float maxSpawn = 0;
	
	
	void OnCollisionEnter2D(Collision2D col){
		float randomLocation = Random.Range(minSpawn,maxSpawn);
		
		if((col.gameObject.tag == "Ball")){
			transform.position.y = randomLocation;
		}
	}
	
}

You have to change the whole position so it’ll be like this:

transform.position = new Vector3(transform.position.x, randomLocation, transform.position.z);