- Home /
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);
}
}
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;
}
}
}
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.
Your answer
Follow this Question
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