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:
- Player presses button
- Clone of prefab appears in front of player
- Player’s script tells the plant’s script to switch a variable (isPlanted) to true
- If isPlanted is true, grow the clone.
Instead, what I have is this:
- Player presses button
- Clone of prefab appears in front of player
- Prefab’s isPlanted is true and begins to grow
- Initial clone’s isPlanted is still false and doesn’t grow
- 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);
}
}