Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Zaliron · Oct 29, 2013 at 09:46 PM · prefabsclones

How do I make a clone differ from the prefab?

I'm starting to make a farming game, and I'm trying to make a basic plant prototype that grows over time. I have a basic prefab "plant" that can be planted by the player multiple times, creating clones each time the player presses a button.

Since I just started and going in small steps, I'm trying to make it so that:
1. Player presses button
2. Clone of prefab appears in front of player
3. Player's script tells the plant's script to switch a variable (isPlanted) to true
4. If isPlanted is true, grow the clone.

Instead, what I have is this:
1. Player presses button
2. Clone of prefab appears in front of player
3. Prefab's isPlanted is true and begins to grow
4. Initial clone's isPlanted is still false and doesn't grow
5. Subsequent cloning causes copies of the growing prefab.

How do I make it so that the prefab's properties never change, only the child's? I can't find a way to access the clone's version of the script instead of the prefab's.

Player's Script:
using UnityEngine; using System.Collections;

 public class CharMovement : MonoBehaviour {
     
     public Plant1 plantScript;
     public Transform plant;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown ("e")){
             Instantiate(plant,transform.position + transform.forward * 5,Quaternion.identity);
             plantScript.isPlanted = true;
         }
             
     }
 }

Plant script:
using UnityEngine; using System.Collections;

 public class Plant1 : MonoBehaviour {
     
     public bool isPlanted;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(isPlanted)
         transform.localScale += new Vector3(0.1F*Time.deltaTime,0.1F*Time.deltaTime,0.1F*Time.deltaTime);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Oct 29, 2013 at 09:48 PM

I think this is what you are looking for:

 public class CharMovement : MonoBehaviour {
 
     public GameObject plant;
 
     void Update () {
        if(Input.GetKeyDown ("e")){
          GameObject go = Instantiate(plant,transform.position + transform.forward * 5,Quaternion.identity) as GameObject;
          go.GetComponent<plantScript>().isPlanted = true;
        }
     }
 }
Comment
Add comment · Show 3 · Share
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
avatar image Zaliron · Oct 29, 2013 at 10:04 PM 0
Share

Unfortunately it gives a null reference exception. =/

avatar image robertbu · Oct 29, 2013 at 10:08 PM 0
Share

Note I changed the type of 'plant' from Transform to GameObject. You will need to drag a new copy of the prefab to the variable in the inspector.

avatar image Zaliron · Oct 29, 2013 at 10:09 PM 0
Share

So you did! And it works, thanks so much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Project's KB weight not influenced by use of prefabs? 0 Answers

Cant delete clones of prefab 1 Answer

Instantiate is spawning too many clones 2 Answers

(Re-posted) How to save multiple of the same prefab clones 0 Answers

instantiated gameobjects(clone) don't behave like its original gameobject. 1 Answer


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