Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 03, 2018 at 02:50 PM by Wi8games for the following reason:

Other

avatar image
0
Question by Wi8games · Aug 19, 2017 at 08:58 PM · prefabassign

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;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Asign prefab to game object based on tag 1 Answer

Instantiate a prefab and assign it's values 1 Answer

Assign a prefab as childs to several gameobjects 1 Answer

[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer

Prefabs getting deleted in all scenes! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges