I keep getting this error even when i’ve restarted Unity, i also copied Gary Pettie’s code for the lecture (Lecture: Currency system (Part 1).
What should i do?
Show us the code. Then we can debug
Please copy/paste your code in a blockquote like this to make it easier to read.
Please do not post a screen-grab of your code.
There are multiple scripts…
Sure, but you have an error on one of them. The message will tell you which one it is
Ok, one sec.
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMover : MonoBehaviour
{
[SerializeField] List<Waypoint> path = new List<Waypoint>();
[SerializeField] [Range(0f, 5f)] float speed = 1f;
Enemy enemy;
void OnEnable()
{
FindPath();
ReturnToStart();
StartCoroutine(FollowPath());
}
void Start()
{
enemy = GetComponent<Enemy>();
}
void FindPath()
{
path.Clear();
GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");
foreach(GameObject waypoint in waypoints)
{
path.Add(waypoint.GetComponent<Waypoint>());
}
}
void ReturnToStart()
{
transform.position = path[0].transform.position;
}
IEnumerator FollowPath()
{
foreach(Waypoint waypoint in path)
{
Vector3 startPosition = transform.position;
Vector3 endPosition = waypoint.transform.position;
float travelPercent = 0f;
transform.LookAt(endPosition);
while(travelPercent < 1f) {
travelPercent += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
}
enemy.StealGold();
gameObject.SetActive(false);
}
}
Blockquote
This is the EnemyMover script
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] int poolSize = 5;
[SerializeField] float spawnTimer = 1f;
GameObject[] pool;
void Awake()
{
PopulatePool();
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnEnemy());
}
void PopulatePool()
{
pool = new GameObject[poolSize];
for(int i = 0; i < pool.Length; i++)
{
pool[i] = Instantiate(enemyPrefab, transform);
pool[i].SetActive(false);
}
}
void EnableObjectInPool()
{
for(int i = 0; i < pool.Length; i++)
{
if(pool[i].activeInHierarchy == false)
{
pool[i].SetActive(true);
return;
}
}
}
IEnumerator SpawnEnemy()
{
while(true)
{
EnableObjectInPool();
yield return new WaitForSeconds(spawnTimer);
}
}
}
This is the ObjectPool script
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] int goldReward = 25;
[SerializeField] int goldPenalty = 25;
Bank bank;
void Start()
{
bank = FindObjectOfType<Bank>();
}
public void RewardGold()
{
if(bank == null) { return; }
bank.Deposit(goldReward);
}
public void StealGold()
{
if(bank == null) { return; }
bank.Withdraw(goldPenalty);
}
}
Enemy script
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetLocator : MonoBehaviour
{
[SerializeField] Transform weapon;
[SerializeField] ParticleSystem projectileParticles;
[SerializeField] float range = 15f;
Transform target;
void Update()
{
FindClosestTarget();
AimWeapon();
}
void FindClosestTarget()
{
Enemy[] enemies = FindObjectsOfType<Enemy>();
Transform closestTarget = null;
float maxDistance = Mathf.Infinity;
foreach(Enemy enemy in enemies)
{
float targetDistance = Vector3.Distance(transform.position, enemy.transform.position);
if(targetDistance < maxDistance)
{
closestTarget = enemy.transform;
maxDistance = targetDistance;
}
}
target = closestTarget;
}
void AimWeapon()
{
float targetDistance = Vector3.Distance(transform.position, target.position);
weapon.LookAt(target);
if(targetDistance < range)
{
Attack(true);
}
else
{
Attack(false);
}
}
void Attack(bool isActive)
{
var emissionModule = projectileParticles.emission;
emissionModule.enabled = isActive;
}
}
Target locator script
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bank : MonoBehaviour
{
[SerializeField] int startingBalance = 150;
[SerializeField] int currentBalance;
public int CurrentBalance {get {return currentBalance;} }
void private void Awake() {
{
currentBalance = startingBalance;
}
}
public void Deposit(int amount)
{
currentBalance += Mathf.abs(amount);
}
public void Withdraw(int amount)
{
currentBalance -= Mathf.abs(amount);
}
}
and bank script
In your Bank
script, you have
void private void Awake()
remove the first void
oh, yeah of course. Forgot to do that. Thank you
Also, you can trigger a blockquote by just using the 'greater than symbol (>).
It will look like this.
You’re definitely on the right track, but I’m sure you can see the difference.
It took me a while to figure it out, too!
Cheers!
Actually, the way to format code is different. Please see the Forum Guide: How to apply code formatting
Oh, hey, you’re right.
Nobody ever told me the right way to do it before. People often just say something like “Do this…” but then they don’t say ‘how’ to actually do it.
Anyway, when the answer is ‘Go figure it out’, I don’t always figure it out the right way.
I literally sat here one day and tried various things in an attempt to get it right and I think I ended up aggravating someone so I stopped trying. I still didn’t feel like I had it quite right, but I just continued with the best option that I had figured out up to that point.
Thank you for finally setting me straight.
No problem, man
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.