Hi all, this is my first post so sorry if I missed any proper way of posting a question.
Basically when I put a gameObject into my serialised field for a script, then make that scripted object a prefab it’s not saving my gameObject. So if I drag in multiple of that prefab the script isn’t working naturally because it has no target.
Also as a side issue, as you can see it’s calling it “Sci Fi Warrior CON” when I drag the script onto my object, which is especially weird when there’s no script with that name in this project. I do know it’s a script in a different project I have because it’s from the asset store, not sure if it’s related but thought it would be worth mentioning in case it’s part of the problem.
Below is the script I’m using in case it’s needed.
I’d really appreciate any help you guys can give
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SciFiWarriorCON : MonoBehaviour
{
// A reference to the player GameObject.
[SerializeField]
private GameObject player;
// The speed of the character.
[SerializeField]
private float moveSpeed = 1.0f;
// A reference to the animator.
private Animator anim;
// Store the current AnimatorStateInfo.
AnimatorStateInfo currentStateInfo;
// Hashes to animator parameters.
private int speedHash = Animator.StringToHash("Speed");
private int dyingHash = Animator.StringToHash("IsDead");
//Check to destroy Zombie after death to clear clutter and save FPS
private bool cleanupCheck = false;
private float cleanup = 5f;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
ZombieHunt();
//CleanUp();
}
private void ZombieMove()
{
if (Vector3.Distance(transform.position, player.transform.position) < 7)
{
anim.SetFloat(speedHash, 0.7f);
Vector3 tmp = player.transform.position;
tmp.y = this.transform.position.y;
transform.LookAt(tmp, Vector3.up);
currentStateInfo = anim.GetCurrentAnimatorStateInfo(0);
if (currentStateInfo.IsName("Walk"))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
else
{
anim.SetFloat(speedHash, 0);
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Trigger: " + other.gameObject.name);
if (other.gameObject.tag.Contains("Projectile"))
{
anim.SetBool(dyingHash, true);
Destroy(other.gameObject);
ScoreManager.ScoreUpdateEvent.Invoke(1);
Destroy(gameObject, 5);
}
}
private void ZombieHunt()
{
anim.SetFloat(speedHash, 0.7f);
Vector3 tmp = player.transform.position;
tmp.y = this.transform.position.y;
transform.LookAt(tmp, Vector3.up);
currentStateInfo = anim.GetCurrentAnimatorStateInfo(0);
if (currentStateInfo.IsName("Walk"))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
}
A Prefab cannot have any references to any object OUTSIDE the hierarchy of the prefab. A Prefab is saved with all the components on itself. A Prefab is saved with all the children it contains and their components too. But it doesn’t save its parent or any other object outside. Those have to be attached/supplied after you Instantiate() the prefab into a scene.
As for how it looks in the Inspector, they try to make things more friendly by inserting spaces between words. This is why your “SciFiWarriorCON” class name became “Sci Fi Warrior CON” in the Inspector. This is done for field names too.
Thanks! How would I reference the player gameObject? When I google it it just talks about assigning it in the editor. I don’t need you to write out the code yourself but if you can provide a link or even the correct thing to google I’m happy to read up on it myself
Assigning in the editor works when everything is in the same scene. However, we can’t reference things across scenes, nor can objects in your assets reference anything within scenes (as scenes are transient).
For something like the player, if there is only ever one player, often a singleton pattern is used. Other folks might use scriptable objects to communicate data.
Right but are your zombies prefabs in your assets? Or are these prefab instances in your scene? If they are the former, they can’t reference your player in the scene. Additionally, if they were to reference some player prefab, this is a different instance to the player in your scene.
Solved it, very simple, I just used GameObject.Find(“PlayerCapsule”) and that did the job, I appreciate the explanations it’s at least helped me understand how Unity works a little better
In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY. If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.