Unity How to get transform from Hashset<EnemyScript> - unity3d

I have created a Hashset to which I add my enemies to. In the Enemy script in the OnEnable and Disable I add and remove from the Hashset. The Hashset is a public static so I can access the count of it by just Debug.Log(Enemy.enemyTargets.Count); to see the number increase and decrease.
How do I access the transform of the Hashset, I know hashset have no specific order and that is fine but I just want the transform of whichever object inside the Hashset.
Inside Enemy.cs script
public class Enemy: MonoBehaviour {
public static readonly HashSet<Enemy> enemyTargets = new HashSet<Enemy>();
private Transform _transform;
public new Transform transform => _transform = _transform ? _transform : base.transform;
private void OnEnable()
{
enemyTargets .Add(this);
}
private void OnDisable()
{
enemyTargets .Remove(this);
}
}

A HashSet has no transform but rather each Enemy has its own .transform reference.
Don't override that property but rather simply use
foreach(var enemy in Enemy.enemyTargets)
{
var transform = enemy.transform;
// use each transform
}
You can also simplify it with Linq
foreach(var transform in Enemy.enemyTargets.Select(enemy => enemy.transform)
{
// use transform
}
Or you could add a property for it like
public static Transform[] enemyTargetTransforms => enemyTargets.Select(enemy => enemy.transform).ToArray();

Related

Need some help getting a component in a multiple child instances of an abstract class

Hiya - so i think i'm at a bit of a misunderstanding here with using abstract classes so if it isn't too much trouble i need someone to explain to me where i've gone wrong here;
So i have an abstract class structured like this:
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Collider2D))]
public abstract class Enemy : MonoBehaviour {
// Physics
protected Rigidbody2D rb;
protected Collider2D col;
protected virtual void Awake(){
// Physics
rb = this.GetComponent<Rigidbody2D>();
col = this.GetComponent<Collider2D>();
col.isTrigger = true;
}
}
Then a class inheriting it:
public class Whizzer : Enemy {
protected override void Awake(){
// Physics
rb = GetComponent<Rigidbody2D>();
rb.bodyType = RigidbodyType2D.Dynamic;
rb.gravityScale = 0f;
col = GetComponent<Collider2D>();
col.isTrigger = true;
}
}
However the problem i'm having is that when i reference rb or col inside the Whizzer class it's (i think) accessing the variables defined in the abstract Enemy class - this results in only one instance of a gameobject with this script working at a time since the last script to run assigns their rigidbody and collider as the variables in the abstract Enemy class
My question is how can i create seperate instances of the variables rb and col for every Whizzer class instance created whilst keeping the variables desired inside the abstract enemy class?
Or if this is even the right way to go about doing this?
Thankyou in advance!
They should be saparate. If you want a variable to be shared among all instances you should use "static" modifier. But you do not use that so each instance of Enemy (Whizzer is also an Enemy) should have it's own value for these fields. How do you know that they point to the same instances of collider and rigidbody?
About your implementation you are slightly missing the point:
public abstract class Enemy : MonoBehaviour {
protected Rigidbody2D rb;
protected Collider2D col;
protected virtual void Awake(){
rb = this.GetComponent<Rigidbody2D>();
col = this.GetComponent<Collider2D>();
col.isTrigger = true;
}
}
Then a class inheriting it:
public class Whizzer : Enemy {
protected override void Awake(){
base.Awake();
rb.bodyType = RigidbodyType2D.Dynamic;
rb.gravityScale = 0f;
}
}
That makes more sense imho, you should use base method in most cases instead of pasting the same chunk of code to override

How to reference a gameObject inside a prefab? Using GameObject.Find does not work

I am trying to use GameObject.Find within a prefab to reference an object that exists in the scene. The name of the object is exactly the same as the actual object.
LoseLives loseLives;
GameObject loseLivesing;
private void Awake()
{
loseLivesing = GameObject.Find("MasterManager");
}
private void Start()
{
loseLives = loseLivesing.GetComponent<LoseLives>();
target = Waypoints.waypoints[0];
}
loseLives.LoseLife();
Null Reference
I get a null reference when "loseLives.LoseLife();" runs.
Any help with solving the no reference problem would be greatly appreciated!
If you think there is a better way to create the connection between a script on a prefab and a script on a in-scene gameObject could you please explain it or point me to a tutorial, because in all my research I did not have any luck.
Thank you!
It seems like your LoseLives is a singular, constant object.
First Method
Simply making use of the inspector by exposing the fields.
public class YourScript : MonoBehaviour {
[SerializeField]
private LoseLives loseLives;
// ..
private void Start() {
loseLives.LoseLife();
}
// ..
}
It comes with the problem of needing to constantly drag your constant object into the inspector whenever you need it.
For a Prefab, it works if the target object is part of the Prefab, or the Prefab is generated in the same scene as the targeted object.
Second Method
Using singletons.
LoseLives.cs
public class LoseLives : MonoBehaviour {
public static LoseLives Instance {
get => Instance;
}
private static LoseLives instance;
private void Start() {
instance = this;
}
public void LoseLife() {
// ...
}
}
YourScript.cs
public class YourScript : MonoBehaviour {
// ..
private void Start() {
LoseLives.Instance.LoseLife();
}
// ..
}
This works if you have a global, singular instance of LoseLives object.
Even though it might not be the best approach, I found a workaround for my issue.
I am using a static variable that resides in the other script (CurrencyManager), calling it from the LoseLives script.
Instead of:
loseLives.LoseLife();
I do:
CurrencyManager.playerHealth -= 1;
Where CurrencyMananger is the other script and playerHealth is a static variable that is in the CurrencyManager script. This works because a static variable does not require the objects to be referenced in the inspector.
From now on, when I have a problem where I want to reference a gameObject within a script that is on a prefab, I will consider using static variables in my solution.

Unity - issue with AudioClip array

I'm trying to make an audio player in Unity.
I first created a ResourceManager, that has a
AudioClip[] array.
public class ResourceManager : MonoBehaviour
{
public AudioSource audioSrc;
public AudioClip[] clips;
private int trackNum = 0;
public void Awake()
{
audioSrc = GetComponent<AudioSource>();
audioSrc.Stop();
...omitted...
public void LoadClip()
{
audioSrc.clip = clips[trackNum];
}
}
So that they appear in the Clips drag-and-drop box.
However, I want to make the AudioClip array static in ResourceManager static, so other scripts (e.g. PlayPause.cs can access clips statically (via function calls).
But the problem is once I make clips static, the drag-and-drop for disappears.
Is there a way to fix that? Or are there some better design patterns? Thanks.
You can e.g. simply do
public class ResourceManager : MonoBehaviour
{
...
// This is the one in the Inspector
// Usually I keep the Inspector private and only provide public access
// where needed via properties
[SerializeField] private AudioClip[] clips;
// This will be set in Awake to the ones from the Inspector
// readonly for others, only this class can assign it
public static AudioClip[] Clips { get; private set; }
public void Awake()
{
Clips = clips;
...
}
}
The alternative suggested is make the ResourceManager a so called singleton like e.g.
public class ResourceManager : MonoBehaviour
{
// Here you store your own instance reference
// and provide public readonly access
public static ResourceManager Instance { get; private set; }
[SerializeField] private AudioClip[] clips;
public AudioClip[] Clips => clips;
...
public void Awake()
{
// Make sure only one instance exists at a time
if(Instance && Instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
// optional: Don't destroy this instance when the scene is switched
// Careful: Make sure this applies also to the AudioSource instance!
// For now I assume it is either attached to this object or a child
// if not simply do the same DontDestroyOnLoad on it as well
DontDestroyOnLoad (this.gameObject);
...
}
public void LoadClip()
{
audioSrc.clip = clips[trackNum];
}
}
And then access everywhere ReaourcesManager.Instance.Clips
Or a built-in alternative to the singleton is using FindObjectOfType so any other script in the Scene can actually simply access
FindObjectOfType<ResourcesManager>().clips
without you having to change anything at all ;)
Except as said I would make it
[SerializeField] private AudioClip[] clips;
public IReadonlyList<AudioClip> Clips => clips;
depending a bit of course what exactly you want to do with the clips but this way the array can not be altered by any other script

Instantiating random or chosen prefabs with sub-container thru Factory or Pool

I have an array of prefabs and I want to be able to Instantiate randomly picked prefabs thru Zenject Factory and perform their bindings in their sub-containers.
What I want to do is the same as in this code sample from Zenject documentation, but for randomly selected prefabs.
https://github.com/modesttree/Zenject/blob/master/Documentation/SubContainers.md#using-game-object-contexts-no-monobehaviours
using UnityEngine;
using Zenject;
public class GameInstaller : MonoInstaller
{
[SerializeField]
GameObject ShipPrefab;
public override void InstallBindings()
{
Container.BindInterfacesTo<GameRunner>().AsSingle();
Container.BindFactory<float, ShipFacade, ShipFacade.Factory>()
.FromSubContainerResolve().ByNewPrefabInstaller<ShipInstaller>(ShipPrefab);
}
}
I was able to partially make it work with
[SerializeField] private GameObject[] ships;
...
Container.BindFactory<float, ShipFacade, ShipFacade.Factory>()
.FromSubContainerResolve().ByNewGameObjectMethod(SpawnShip);
...
private void SpawnShip(DiContainer container, float speed)
{
container.Bind<ShipFacade>().AsSingle();
container.Bind<Transform>().FromComponentOnRoot();
var shipPrefab = ships[Random.Range(0, ships.Length)];
var ship = container.InstantiatePrefab(shipPrefab);
container.Bind<ShipHealthHandler>().FromNewComponentOn(ship).WhenInjectedInto<ShipFacade>();
container.BindInstance(speed).WhenInjectedInto<ShipInputHandler>();
}
But it's awkward and in this case I guess I'm not using an advantage of sub-container. And also prefabs spawns in an empty GameObject.
What I want to achieve is to be able to use ShipInstaller for sub-container binding.
You're right, there wasn't really a very elegant way to choose the sub-container prefab dynamically.
I took some time to make this better today with this commit. If you use the latest version of Extenject then you can now do things like this:
public class QuxInstaller : Installer {
float _speed;
public QuxInstaller(float speed) {
_speed = speed;
}
public override void InstallBindings() {
Container.BindInstance(_speed);
Container.Bind<QuxFacade>().AsSingle();
}
}
public class CubeInstaller : MonoInstaller
{
public List<GameObject> QuxPrefabs;
public override void InstallBindings()
{
Container.BindFactory<float, QuxFacade, QuxFacade.Factory>()
.FromSubContainerResolve().ByNewPrefabInstaller<QuxInstaller>(ChooseQuxPrefab);
}
UnityEngine.Object ChooseQuxPrefab(InjectContext _) {
return QuxPrefabs[Random.Range(0, QuxPrefabs.Count)];
}
}

How to display & modify array in the Editor Window?

I have GameObject array field in my CustomEditor class derived from the UnityEngine.Editor. I need to be able to display (draw) and give user ability to modify that array.
Just like how Unity's Inspector doing this for Serializable fields of objects derived from the ScriptableObject. E.g. displaying materials array in the inspector:
Refer to yours editor object as to the SerializedObject and then find any required property, draw it, and apply modification:
public class MyEditorWindow : EditorWindow
{
[MenuItem("Window/My Editor Window")]
public static void ShowWindow()
{
GetWindow<MyEditorWindow>();
}
public string[] Strings = { "Larry", "Curly", "Moe" };
void OnGUI()
{
// "target" can be any class derived from ScriptableObject
// (could be EditorWindow, MonoBehaviour, etc)
ScriptableObject target = this;
SerializedObject so = new SerializedObject(target);
SerializedProperty stringsProperty = so.FindProperty("Strings");
EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
so.ApplyModifiedProperties(); // Remember to apply modified properties
}
}
Original answer here.
I managed to do it with a for loop and an extra int:
using UnityEditor;
using UnityEngine;
public class RockSpawner : EditorWindow
{
int rockCollectionSize = 0;
GameObject[] rockCollection;
[MenuItem("Tools/Rock Spawner")]
public static void ShowWindow()
{
GetWindow(typeof(RockSpawner));
}
void OnGUI()
{
rockCollectionSize = EditorGUILayout.IntField("Rock collection size", rockCollectionSize);
if (rockCollection != null && rockCollectionSize != rockCollection.Length)
rockCollection = new GameObject[rockCollectionSize];
for (int i = 0; i < rockCollectionSize; i++)
{
rockCollection[i] = EditorGUILayout.ObjectField("Rock " + i.ToString(), rockCollection[i], typeof(GameObject), false) as GameObject;
}
}
}
The top answer didn't work well for me because the properties did not update, there for I moved the declaration lines to the OnEnable function you do not want to declare them over and over again) and used to so.Update() to update the changed variables.
using UnityEngine;
using UnityEditor;
public class MyEditorWindow : EditorWindow
{
[MenuItem("Window/My Editor Window")]
public static void ShowWindow()
{
GetWindow<MyEditorWindow>();
}
public string[] Strings = { "Larry", "Curly", "Moe" };
SerializedObject so;
private void OnEnable()
{
ScriptableObject target = this;
so = new SerializedObject(target);
}
void OnGUI()
{
// "target" can be any class derived from ScriptableObject
// (could be EditorWindow, MonoBehaviour, etc)
so.Update();
SerializedProperty stringsProperty = so.FindProperty("Strings");
EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
so.ApplyModifiedProperties(); // Remember to apply modified properties
}
}
Links:
Base for my answer
SerializedObject.Update
public GameObject[] yourGameObjects;
Then in the inspector set your size, and fields should open up.
add a public array to your script
public GameObject[] myObjects