Plant and gather! :D

Hello,

I made a little JavaScript for Planting and gathering,
Now this is my script;

Onpickup.js

#pragma strict

public var cube : Transform;

var seedsCount : UI.Text;
var plantCount : UI.Text;

private var playerPos:Vector3;
private var spawnPos:Vector3;
public var player: GameObject;

var SAmount = 5;
var Plant = 0;
//Gather
var gatherDistance = 2;
var gatherTime = 1;

//Boolean
var inRange = false;

function Start () {

}

function Update () 
{
    seedsCount.text = "Seeds: " + SAmount;
    plantCount.text = "Planted: " + Plant;
    //Gather
    var player : Vector3 = gameObject.Find("sux(clone)").transform.position;
    var moveDirection : Vector3 = player - transform.position;
   
    if(moveDirection.magnitude < gatherDistance)
    {
        inRange = true;
    }
    else if(moveDirection.magnitude > gatherDistance)
    {
        inRange = false;
    }
    if(inRange == true)
    {
        if(Input.GetKeyDown(KeyCode.J))
        {
            print("J is pressed");
            DoGather();
        }
    }
    if(Input.GetKeyDown(KeyCode.K))
    {
        print("K is pressed");
        DoPlant();
    }
}

function OnTriggerEnter(col : Collider)
{
    if(col.gameObject.name == "Cube")
    {
        print("Cube");
        Destroy(col.gameObject);
    }
}

function DoPlant()
{
    if(SAmount == 0)
    {
    print("Not enough seeds!");
    }
    else if(SAmount > 0)
    {
    playerPos = player.transform.position;
    spawnPos = Vector3(playerPos.x, playerPos.y-0.5, playerPos.z); ///change as needed
    Instantiate (cube, spawnPos, Quaternion.identity);
    SAmount -= 1;
    Plant += 1;
    }
}

function DoGather()
{
    yield WaitForSeconds(gatherTime);
    Destroy(gameObject);
    Plant -= 1;
}

It is attached to my Character,
The planting works, but the problem is the gathering.
It shows me this error;

^ Thats another script that used to work BEFORE i modified the onpickup.js script

So basically what the Gather should do; Check if Character is near by the plant, if in between 2, then inRange goes too true and then i can press J too gather it.

Hi, Whats in the “TreeChop.js” script?

ill show u;
[THIS SCRIPT IS ATTACHED TO THE TREES]

#pragma strict
var chopDistance = 10;
var chopTime = 5;

//boolean
var inRange = false;

function Start () {

}

function Update ()
{
    var player : Vector3 = gameObject.Find("Character").transform.position;
    var moveDirection : Vector3 = player - transform.position;
  
    if(moveDirection.magnitude < chopDistance)
    {
        inRange = true;
    }
    else if(moveDirection.magnitude > chopDistance)
    {
        inRange = false;
    }
  
    if(inRange == true)
    {
        if(Input.GetKeyUp("f"))
        {
            Timer();
        }
    }
}

function Timer()
{
    yield WaitForSeconds(chopTime);
    Destroy(gameObject);
    Player_Inventory.itemPlayersAmount[0] += 1;
    Inventory_Add_Item.InventoryNewItemAdded = 0;
    print(Player_Inventory.itemPlayersAmount[0]);
}

It worked before that script

Does the “Character” still exist? It tries to find: gameObject.Find(“Character”)

Yeah, the Character is my player it still exists :slight_smile: The Chopscript works perfect. without the other script modified.