I have a prefab for a bullet the enemies shoot. I have the bullet’s path dictated by a script on the bullet (bulletpath). Basically, it looks at the position of the bullet when it’s instantiated, and the position of the player, and forms a vector3 to make the velocity, so the bullet will fly at the player. This works extremely well, if the player is alive. When the player is dead, bulletpath obviously has no reference for what object to plug into its math, so I get flooded with errors, which makes sense.
What I want to do is make an if statement to check if the player is alive, if so, then the math plugs the player position in. If the player isn’t active, then it will use a cube I’ve made past the starting position, and basically aim at that. All of this math makes sense for me and what I’m doing, it’s just I’m having a problem getting the prefab to identify whether the player object is active.
My question is this, I want to make a [SerializedField] of a gameobject in the bulletpath script, which is attached to the bullet prefab. However, when going into my prefab to edit it, I no longer have access to the main hierarchy, so I cannot drag my player object into the serializedfield of my bulletpath script on my bullet prefab.
I’ve tried creating a new variable in another random script outside the prefab that will identify the player, then I could call on that within the prefab. The problem is, if it’s no “static public” then I cannot access it, and if it is “static public” then I can’t use the serializedfield to identify which game object I want it to be…
I’ve also tried using “FindObjectOfType()” to find my player object (ShipControls is a script that only my player object has on it), but it won’t allow me to do this for telling a gameobject variable what gameobject it references (even though it allows me to do this for a gameobject’s transform values, which makes no sense to me). It says I cannot implicitly convert type ‘ShipControls’ to ‘UnityEngine.GameObject’. That makes no sense to me, because it should find the thing that has ‘ShipControls’, which is a GameObject, so I definitely can use a GameObject to be a GameObject.
I’m getting very confused the more I try to figure out a way to do this. Basically, I want to know how to either.
- Plug in a value for a serializedfield in a prefab from the main hierarchy. or
- Reference a gameobject purely using script.
I guess? Thanks for any help you can provide.
(Also, why does this site totally ruin the formatting of what I type? It ignores all the times I pushed enter…?)