Unity with ML Agents - OnActionReceived is always empty - unity3d

My code is really simple :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
public class move : Agent
{
public override void OnActionReceived(ActionBuffers actions)
{
Debug.Log(actions.DiscreteActions[0]);
}
}
I have "Discrete Actions" enabled in the Behavior Parameters and I'm using 1000 Max Steps. I'm using a simple Cube as a GameObject with those 2 scripts attached - the Behavior Parameters and the test script. I am running "mlagents--learn" and I get no errors or warnings it cannot connect (I do if I don't run mlagents first, so I'm asuming it has a connection). Is there any reason why I never get any actions? It's been giving me a headache for a few days, help would be greatly appreciated.
Thank you!

Related

Unity script stops working when scene is reloaded

I am trying to create an infinite runner.
My scene have a "road creator" object that is just a trigger and a script attached to my road prefab.
Every time a piece of road leaves the road creator trigger, a new piece of road is created.
Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class roadGenerator : MonoBehaviour
{
public GameObject road;
private void OnTriggerExit(Collider other) {
if(other.gameObject.tag == "roadGenerator"){
Instantiate(road, new Vector3(200, 0, 0), Quaternion.identity);
}
}
}
My issue is that once the player loses and the level reloads the script stops working.
Everything gets loaded properly (the trigger object and the first piece of road with the script attached) but for some reason the script doesn't get triggered...
Hey I think I'll need some more context to understand your problem:
You have a "roadGenerator" with a trigger and the Script attached + a "Road" Prefab with the Tag "roadGenerator" right?
I suppose by "realoading the level" you mean reloading the Scene with something like SceneManager.LoadScene(...)
It would also be good to know what you already tried out to fix it. :)

In Unity3D, how can I stop the editor from executing an editor extention script that is stuck in an error loop?

In Unity3D, I have created the below editor extension and it worked but got stuck in an endless error loop. How can I stop the editor from executing it? I have tried Ctrl-c and Ctrl-z but no luck. I would prefer not to restart the editor but I can do that if it is the only way.
using UnityEngine;
using UnityEditor;
public class ConfigureLevelPart : Editor
{
[MenuItem("GameObject/AddCollidersToChildren", false, 0)]
static void testFunction1()
{
foreach (Transform child in Selection.activeGameObject.transform)
{
if (child.GetComponent<MeshRenderer>())
{
child.gameObject.AddComponent<MeshCollider>();
child.gameObject.isStatic = true;
child.gameObject.tag = "World";
child.gameObject.layer = 12; //layer 12 is world
}
}
}
}
The errors are:
[PathTracer] AddGeometry job with hash: 151d6786c86f1a0497062c3ce74bf71b failed with exit code 1.
and
[PathTracer] Failed to add geometry; mesh is missing required attribute. Please make sure mesh contains positions, normals and texcoord0.
and these just keep repeating.
Answering my own question as I found out the specific issue in this case. However I still do not have a general solution.
In my case, my editor script triggered lighting to re-bake because I had auto-generate turned on. There must be an error in my scene and this caused an endless error loop for some reason. My temporary solution was to go into the lighting settings and turn off auto-generate and cancel the bake.

Best practice to to access components using scripts in Unity?

I am new to Unity so go easy on me. :)
I added an game object with a text field component (via TextMeshProUGUI) to my heads up display in my scene. I want to use this to display various statistics on it for debugging purposes during game play.
I then created a script which I added as a component to the same game object that holds my text component. Is this the best practice? Not sure how else I would get the script to execute.
Once I had my script created, I needed to find the text component as well as some other components in my scene so I could display the debug information. Below you can see how I did it... it feels a little dirty to be searching the entire scene to find these things. Would love some insight on how long-time Unity programmers go about this!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PlayerDebugStatistics:MonoBehaviour {
TextMeshProUGUI playerDebugStatisticsText;
PlayerCharacterController playerCharacterController;
Health playerHealth;
private void Start() {
// First find the object in the scene named "PlayerDebugStatisticsText" and then get it's TextMeshProUGUI component
this.playerDebugStatisticsText = GameObject.Find("PlayerDebugStatisticsText").GetComponent<TextMeshProUGUI>();
// Get the player character controller
this.playerCharacterController = GameObject.FindObjectOfType<PlayerCharacterController>();
// Get the player health from the player character controller
this.playerHealth = playerCharacterController.GetComponent<Health>();
}
void Update() {
// Update the text every frame
this.playerDebugStatisticsText.SetText(string.Format("{0:N2}", this.playerHealth.currentHealth));
}
}
3 ways
Create an inspector reference to the other object and access it from your script - a public or private (with [SerializeField]) attribute in your script - and drag the component in like in this video: https://youtu.be/dMgQOP7kdxg?t=425
Use the singleton pattern: https://www.youtube.com/watch?v=5p2JlI7PV1w
Use dependency injection - https://github.com/modesttree/Zenject
Your way isn't terrible if you don't do it in the Update() loop, and cache your objects (like it appears you are doing), but if you need that performance in Start(), the above options can help.

How to switch scenes using the OVR Utilities?

I'm trying to switch scenes when an object in a VR environment is pressed. Should be a simple line of code but when I try to execute it the game crashes. The game is build to the Oculus Go.
I know I've added the scenes to the build to that shouldn't be the problem. I also got the index of '1' right in the build settings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SphereScript : MonoBehaviour
{
public void LoadScene()
{
SceneManager.LoadScene("1");
}
}
private void ProcessTouchpadDown()
{
if (!m_CurrentObject)
return;
Interactable interactable = m_CurrentObject.GetComponent<Interactable>();
CubeScript.onVRTriggerDown();
SphereScript.LoadScene();
}
}
There seems to be a small mistake in SceneManager.LoadScene("1");. If you want to load a scene by its built number and not by its name, you'll have to put in an integer and not a string. So unless your scene is named "1", this won't do the trick. Try SceneManager.LoadScene(1); instead.

Unity PlayerPrefs does not exist in the current context

I am getting errors using PlayerPrefs in Unity 4.6. I keep getting the error message saying "PlayerPrefs does not exist in the current context." I get the same message in MonoDevelop when I move the mouse pointer to hover over the PlayerPrefs statement. Any suggestions are appreciated.
maybe you are not in a true context means not in a class extended from monoBehavious
your desired code should look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KiaStart : MonoBehaviour
{
// you can use PlayerPrefs here!...
void Start()
{
}
void Update()
{
}
}