The goal of this script:
Imagine a delivery game. You have plenty of houses around (100+). You need to go to house A (origin house) to pick a package, then go to house B (target house) to deliver the package.
There is an object in my game scene called “DELIVERY MANAGER” with the following script attached. Player already moves fine in the scene, and so does camera/light. All houses have solid colliders. I want the player to be able to pick or deliver the package if he is close enough to the house.
ALL ADVICE APPRECIATED INCLUDING TO CLEAR MY CODE i know it’s a mess!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class DeliveryPicker : MonoBehaviour
{
public Transform playerPosition;
public TextMeshProUGUI textToPlayer;
public GameObject gameObject;
//public Renderer sphereRenderer;
public GameManager gameManager;
public GameObject[] prefabDelivery;
public int randomHouse;
public int randomHouse2;
public GameObject targetHouse;
public GameObject originHouse;
public Material green;
public Material red;
public Material white;
public int cashReward;
public bool deliveryActive = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!gameManager.hasDelivery && !deliveryActive)
{
randomHouse = Random.Range(0, prefabDelivery.Length);
originHouse = prefabDelivery[randomHouse];
// public Renderer originHouseColor;
originHouse.GetComponent<MeshRenderer>().material = green;
gameManager.hasDelivery = true;
}
else
{
if (playerPosition && !deliveryActive)
{
float dist = Vector3.Distance(playerPosition.position, originHouse.transform.position);
if (dist < 15.0f && Input.GetKeyDown(KeyCode.E) && gameManager.questActive == false)
{
textToPlayer.text = "You got a delivery! Deliver to the house in green!";
PickTarget();
deliveryActive = true;
}
}
}
if (deliveryActive)
{
if (playerPosition)
{
float dist = Vector3.Distance(playerPosition.position, targetHouse.transform.position);
if (dist < 15.0f && Input.GetKeyDown(KeyCode.E) && gameManager.questActive == false)
{
textToPlayer.text = "Package Delivered! Enjoy the money!";
cashReward = Random.Range(95, 250);
gameManager.UpdateCash(cashReward);
deliveryActive = false;
gameManager.hasDelivery = false;
}
}
}
}
void PickTarget()
{
randomHouse2 = Random.Range(0, prefabDelivery.Length);
while (randomHouse2 == randomHouse) { randomHouse2 = Random.Range(0, prefabDelivery.Length); }
targetHouse = prefabDelivery[randomHouse2];
originHouse.GetComponent<MeshRenderer>().material = green;
}
}
Imaginge a scene with 100+ houses. If no deliveries are active, the script should pick a house, make it green so the player knows where to pick the package, and allow the player to pick up the package by coming close to the House and pressing E. Player also needs not to be on a quest.
Then the house the player picked his package from should go back to its original color (not implemented yet!) while the target house should turn green. Getting near the target house and pressing E should reward the player by a) awarding him cash b) generating another house to pick a package from, and turning that house green.
BUT NOTHING WORKS!!! Either nothing happens at all, or I can’t interact with anything!!! No error in compiler. Spent all night on this code and nothing works. At this point its 1AM, ive been working all day and I cant even think anymore. NOT giving up, but I’d appreciate help. I’ll continue this script tomorrow.
This is my first “real” code (check my old post to see how I progressed lol). All advice/rating/anything appreciated, thank you