I have an embedded Player class.
public class PlayerInstaller : MonoInstaller
{
[SerializeField] private GameObject _playerPrefabs;
public override void InstallBindings()
{
var player = Container.InstantiatePrefabForComponent<Player>(_playerPrefabs);
Container.Bind<Player>().FromInstance(player).AsSingle();
}
}
But right now it not work too, but it worked before
I'm trying to inject a second class from the same instance:
Container.Bind<PlayerMovement>().FromComponentOn(player.gameObject).AsSingle()
and many-many options
but it still throws an error
ZenjectException: Unable to resolve 'PlayerMovement' while building object with type 'Flipper'. Object graph: Flipper
Related
I'm creating an enemy (from EnemyCreator1 class) with both EnemyMove1 and MarkusEnemy scripts (EnemyMove1 is a parent class to MarkusEnemy class). In EnemyCreator1 class I set value mainState of the script EnemyMove1 to "CHASE", but when I'm trying to access it from that class it says that mainState is "IDLE" (Please read my coments below because there are more explanations about what am I trying to achieve)
public class EnemyMove1 : MonoBehaviour
{
public enum mainStates { IDLE, CHASE }
public mainStates mainState;
void Update()
{
Debug.Log(mainState); //mainstate == IDLE, but should be CHASE
}
}
public class EnemyCreator1 : MonoBehaviour
{
[SerializeField] private GameObject enemyPrefab;
public void CreateEnemyAndSetItsStateToChase()
{
GameObject enemy = Instantiate(enemyPrefab);
enemy.GetComponent<EnemyMove1>().mainState = EnemyMove1.mainStates.CHASE;
}
}
public class MarkusEnemy : EnemyMove1
{
void Update()
{
EnemyMove enemyMoveScript = GetComponent<EnemyMove>();
Debug.Log(enemyMoveScript.mainState); //mainstate == CHASE
}
}
From the above code it looks like you are inheriting from a different base class EnemyMove, not EnemyMove1.
Thank you guys for helping me, after searching for the information about base classes I decided that it is impossible to access directly it's variables from another objects' scripts so I just simply call methods with variables as arguments (I put variables in round brackets of the method)
I've been trying to perfectly structure this project I'm working on in different classes while maximizing the benefits of inheritance. So far however, it's given me more headaches than benefits.
Consider this:
public class SuperClass : MonoBehaviour
{
[SerializeField] protected Camera _camera;
}
and this
public class SubClass : SuperClass
{
}
Both scripts are attached to different game objects in the scene.
The Camera is to be assigned by dragging it in the inspector
I tried this, and unity seemed to tell me that I had to assign the camera to the SuperClass game object AND to the subclass game object, which makes no sense to me.
How can I assign a camera to SuperClass.cs, which is then used and shared by all of its subclasses?
Thanks in advance!
shared by all of its subclasses
Shared by classes could can only be achieved by using "static" (static variable or singleton).
A workaround could be
public class SubClass :SuperClass
{
[SerializeField] Camera camera;
void Awake()
{
if(camera!=null)
{
_camera=camera;
}
}
// Start is called before the first frame update
void Start()
{
camera=_camera;
}
}
To further extend the solution, you could write a editor script or just get the camera from the code.
You need to create public static Camera property somewhere and reference it in your code, using property:
public static class StaticValues
{
public static Camera Camera {get; set;}
}
public class SuperClass : MonoBehaviour
{
[SerializeField] protected Camera _camera
{
get
{
return StaticValues.Camera;
}
set
{
StaticValues.Camera = value;
}
}
}
public class SubClass : SuperClass
{
}
I have a simple service manager called ServiceManager that has two methods. Create() creates an instance of a service. Provide() returns a service that has previously been created.
I have a basic implementation that works but am wondering if there's a cleaner way. This is my basic implementation of the ServiceManager:
public class ServiceManager : MonoBehaviour
{
private Dictionary<Type, MonoBehaviour> services = new Dictionary<Type, MonoBehaviour>();
public void Create<T>() where T : MonoBehaviour
{
// Create service
GameObject serviceObject = new GameObject(typeof(T).Name);
serviceObject.transform.SetParent(transform); // make service GO our child
T service = serviceObject.AddComponent<T>(); // attach service to GO
// Register service
services.Add(typeof(T), service);
}
public T Provide<T>() where T : MonoBehaviour
{
return (T)services[typeof(T)]; // notice the cast to T here
}
}
Using the service is simple:
public class ServiceTest : MonoBehaviour
{
private void Start()
{
// Creating services
ServiceManager services = FindObjectOfType<ServiceManager>();
services.Create<MapService>();
services.Create<InteractionService>();
}
private void Example()
{
// Get a service
ServiceManager services = FindObjectOfType<ServiceManager>();
MapService map = services.Provide<MapService>();
// do whatever you want with map
}
}
My question is about ServiceManager.Provide(). Notice the cast to T after getting the item from the dictionary. This feel very unclean and makes me wonder if I am missing something about how generics work in C#. Are there other/better ways to do what I am trying to accomplish?
There is nothing to improve here. The cast is necessary because the dictionary value type is a MonoBehaviour. You know that it is actually T, but the compiler doesn't. You have to tell that by casting.
You did well.
If there is only ever one instance per type, then there is better. Consider a static generic type
using UnityEngine;
public class ServiceManager : MonoBehaviour
{
// If this T confuses you from the generic T used elsewhere, rename it
public static Transform T { get; private set; }
void Awake()
{
T = transform;
}
public T Provide<T>() where T : MonoBehaviour
{
return ServiceMap<T>.service; // no cast required
}
}
static class ServiceMap<T> where T : MonoBehaviour
{
public static readonly T service;
static ServiceMap()
{
// Create service
GameObject serviceObject = new GameObject(typeof(T).Name);
serviceObject.transform.SetParent(ServiceManager.T); // make service GO our child
service = serviceObject.AddComponent<T>(); // attach service to GO
}
}
Using the service is simple:
public class ServiceTest : MonoBehaviour
{
private void Start()
{
// no need to Create services
// They will be created when Provide is first called on them
// Though if you want them up and running at Start, call Provide
// on each here.
}
private void Example()
{
// Get a service
ServiceManager services = FindObjectOfType<ServiceManager>();
MapService map = services.Provide<MapService>();
// do whatever you want with map
}
}
Also, if you have multiple ServiceManagers then this won't work.
I'm having trouble showing a public field of a ScriptableObject which is a child of the component I'm inspecting. While I can easily do this in another way, I need this method to work for other variables. (ReorderableLists)
I simplified the problem, maybe I was just doing something obvious wrong, but I can't see what I'm doing wrong.
Code + error:
http://answers.unity3d.com/storage/temp/70243-error.png
class SomeComponent : MonoBehaviour{
public MyScriptable scriptable; //instantiated and saved as asset
}
[Serializable] class MyScriptable : ScriptableObject{
[SerializeField] public float value = 0.1f;
}
[CustomEditor(typeof(SomeComponent))] class SomeComponentEditor : Editor{
public override void OnInspectorGUI() {
if((target as SomeComponent).scriptable==null) (target as SomeComponent).scriptable = ScriptableObject.CreateInstance(typeof(MyScriptable)) as MyScriptable;
EditorGUILayout.PropertyField(serializedObject.FindProperty("scriptable"));
//shows the asset
EditorGUILayout.PropertyField(serializedObject.FindProperty("scriptable").FindPropertyRelative("value"));
//error
}
}
To fix your code you can do this:
using UnityEditor;
using UnityEngine;
class SomeComponent : MonoBehaviour
{
public MyScriptable myScriptable;
}
class MyScriptable : ScriptableObject
{
public float myChildValue = 0.1f;
}
[CustomEditor(typeof(SomeComponent))]
class SomeComponentEditor : Editor
{
public override void OnInspectorGUI()
{
SomeComponent someComponent = target as SomeComponent;
if (someComponent.myScriptable == null)
someComponent.myScriptable = CreateInstance<MyScriptable>();
SerializedProperty myScriptableProp = serializedObject.FindProperty("myScriptable");
EditorGUILayout.PropertyField(myScriptableProp);
SerializedObject child = new SerializedObject(myScriptableProp.objectReferenceValue);
SerializedProperty myChildValueProp = child.FindProperty("myChildValue");
EditorGUILayout.PropertyField(myChildValueProp);
child.ApplyModifiedProperties();
}
}
To inspect a child object, you first need to create a SerializedObject version of it, which then can be searched for properties as usual.
Also, the Serializable attribute is not needed on classes which derive from ScriptableObject and the SerializeField attribute is only needed when serializing private fields; public fields are serialized by default in Unity.
Without knowing the original context of your code, your approach seems a little peculiar to me. ScriptableObject instances are meant to be used as asset files in your Unity project. Have you used the CreateAssetMenu attribute yet? Usually, you would create your assets manually via the menu and then plug them into your components. The way you are doing it, it won't be written to disk, so why use ScriptableObject and not just a normal class? But maybe it all makes sense in your actual context, so never mind if I'm wrong.
Hack solution found:
SerializedObject newserobj = new SerializedObject(serializedObject.FindProperty("scriptable").objectReferenceValue );
EditorGUILayout.PropertyField(newserobj.FindProperty("value"));
newserobj.ApplyModifiedProperties();
In order to get variable(s), function(s) in another class, I have known 2 ways of doing this. First, is to use Get Component to the Script that we want to get the variable(s), function(s) into. Second, is to use Instance of the Script itself.
So I have made the following code:
First case: Get Component to the Script itself
public class Manager : MonoBehaviour
{
private AnotherManager _anotherManager;
private void Awake()
{
_anotherManager = GameObject.Find("Managers").GetComponent<AnotherManager>();
}
private void Start()
{
_anotherManager.myIntVariable = 10;
_anotherManager.MyFunction();
}
}
public class AnotherManager : MonoBehaviour
{
public int myIntVariable;
public void MyFunction()
{
}
}
Second case: Use Instance of the Script itself
public class Manager : MonoBehaviour
{
private void Start()
{
AnotherManager.instance.myIntVariable = 10;
AnotherManager.instance.MyFunction();
}
}
public class AnotherManager : MonoBehaviour
{
public static AnotherManager instance;
public int myIntVariable;
private void Awake()
{
instance = this;
}
public void MyFunction()
{
}
}
My question is: Is there any difference between those cases? In terms of good practice of coding for programmer or performance or it is just a matter of programmer's perspective or whatever else?
Thanks
The second example is the what is known as the Singleton Pattern and should be used very sparingly.
I try to never use the first approach either where you find the gameobject and hope it exists.
You can expose a field for the Unity Inspector so that you can wire it up the same as you can expose any other variable
public AnotherManager AnotherManager;
Alternatively, if you hate using public all over the place like that, like me, you can also indicate to Unity that you wish to expose this variable in the inspector with the SerializeField attribute
[SerializeField]
private AnotherManager anotherManager;
With both of these methods, you can then drag an an object that has the AnotherManager component attached into the field in the inspector.
If instantiated objects need access to this, you will need to wire it up when it is instantiated.
If you need help attaching it in unity I can attach some screenshots.