Other
How to have a script on multiple objects and assign a prefab of a parent game object
I need help with this script i have, i basically need to assign a prefab to a script that is sitting on multiple game objects but when i assign it on one of them i dont want it to assign the same object to other ones.
Im making a farming game, and when you press on a sign when you have a tomato in your hands, you plant it, and in my script it assigns that parent object to "ToPlant" prefab, But when you take carrots and plant them, it changes that "ToPlant" to carrots. My problem is for tomatoes to grow it takes 15 sec and for carrots it takes 30 sec, and i need one to be assigned to that signs "ToPlant" and another one to be assigned as well, but that they dont change each other.
and im planing to add more vegetables and fruits to the game so this is A MUST to be fixed.
My Script is: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlantController : MonoBehaviour {
public GameObject Player;
public GameObject ToPlant;
public int SpeedOfGrowth = 15;
public bool IsGrowing = false;
void Start()
{
//"/Assets/resources/Prefabs/TomatoStick"
AssignPrefab();
Player = GameObject.Find("FPSController");
IsGrowing = false;
//TomatoTree = GameObject.Find("TomatoStick(Clone)");
ToPlant.GetComponent<PickUp>().enabled = true;
ToPlant.GetComponent<SpawnPlantZone>().enabled = true;
Player.GetComponent<Farming>();
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
}
void Update()
{
GrowthController();
if (Player.GetComponent<Farming>().HoldingTomatos == true)
{
Player.GetComponent<Farming>().HoldingCarrots = false;
AssignPrefab();
}
if (Player.GetComponent<Farming>().HoldingCarrots == true)
{
Player.GetComponent<Farming>().HoldingTomatos = false;
AssignPrefabCarrot();
}
if (ToPlant == null && Player.GetComponent<Farming>().HoldingTomatos == true)
{
AssignPrefab();
}
if (ToPlant == null && Player.GetComponent<Farming>().HoldingCarrots == true)
{
AssignPrefabCarrot();
}
if (GetComponentInChildren<Transform>().gameObject != null)
{
return;
}
if (GetComponentInChildren<Transform>().gameObject == null && ToPlant == null)
{
gameObject.GetComponent<MeshRenderer>().enabled = true;
Sign();
AssignPrefab();
}
//TomatoTree = GameObject.Find("TomatoStick(Clone)");
}
public void OnMouseDown()
{
if (Player.GetComponent<Farming>().HoldingTomatos == true)
{
if(Player.GetComponent<Farming>().Tomatos <= 0)
{
return;
}
PlantIt();
}
if (Player.GetComponent<Farming>().HoldingCarrots == true)
{
if (Player.GetComponent<Farming>().Carrots <= 0)
{
return;
}
PlantIt();
}
}
public void PlantIt()
{
Vector3 SpawnPos = transform.position;
SpawnPos.y -= 0.1f;
if (Player.GetComponent<Farming>().HoldingTomatos == true)
{
Player.GetComponent<Farming>().TakeTomatoSeeds(1);
}
if (Player.GetComponent<Farming>().HoldingCarrots == true)
{
Player.GetComponent<Farming>().TakeCarrotSeeds(1);
}
GameObject go = Instantiate(ToPlant, SpawnPos, transform.rotation) as GameObject;
go.transform.parent = gameObject.transform;
gameObject.GetComponentInChildren<SpawnPlantZone>().enabled = false;
//ToPlant = GameObject.Find("TomatoStick(Clone)");
if (ToPlant != null)
{
StartCoroutine(WaitTillGrown());
return;
}
NoSign();
}
public IEnumerator WaitTillGrown()
{
IsGrowing = true;
NoSign();
gameObject.GetComponentInChildren<SpawnPlantZone>().enabled = false;
gameObject.GetComponentInChildren<PickUp>().enabled = false;
//ToPlant = GameObject.Find("TomatoStick(Clone)");
//ToPlant.GetComponent<PickUp>().enabled = false;
//ToPlant.GetComponent<SpawnPlantZone>().enabled = false;
yield return new WaitForSeconds(SpeedOfGrowth);
IsGrowing = false;
gameObject.GetComponentInChildren<SpawnPlantZone>().enabled = true;
gameObject.GetComponentInChildren<PickUp>().enabled = true;
}
public void NoSign()
{
//foreach (Transform child in transform)
//{
//child.GetComponent<MeshRenderer>().enabled = false;
//}
gameObject.GetComponent<MeshRenderer>().enabled = false;
gameObject.GetComponent<BoxCollider>().enabled = false;
}
public void Sign()
{
//foreach (Transform child in transform)
//{
//child.GetComponent<MeshRenderer>().enabled = true;
//}
Debug.Log("Something");
gameObject.GetComponent<MeshRenderer>().enabled = true;
gameObject.GetComponent<BoxCollider>().enabled = true;
}
public void AssignPrefab()
{
ToPlant = (GameObject)Resources.Load("TomatoStick", typeof(GameObject));
}
public void AssignPrefabCarrot()
{
ToPlant = (GameObject)Resources.Load("Carrots", typeof(GameObject));
}
public void GrowthController()
{
if (gameObject.transform.Find("TomatoStick(Clone)"))
{
AssignPrefab();
SpeedOfGrowth = 15;
}
if (gameObject.transform.Find("Carrots(Clone)"))
{
AssignPrefabCarrot();
SpeedOfGrowth = 30;
}
}
}