Public variable from serialized field for prefab
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. 1) Plug in a value for a serializedfield in a prefab from the main hierarchy. or 2) 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...?)
Answer by DreivanBG · Feb 02, 2020 at 12:26 AM
Hi, I'm new to Unity but i think i see your error when you try to use FindObjectOfType()
.
ShipControls
is not GameObject, if you want to access his methods you need to do something like this:
ShipControls playerShipControls = FindObjectOfType();
After that you can access all public methods my just typing playerShipControls.publicMethodName
I hope that will help you a little bit good luck.
Your answer
![](https://koobas.hobune.stream/wayback/20220612222546im_/https://answers.unity.com/themes/thub/images/avi.jpg)