Unity Slider does not return a value in the Script - user-interface

When I have a UI Slider in Unity and connect a Script function to this element.
As you can see here:
http://imgur.com/a/zPMEj
I have a Gameobject called "SettingsMenu" with a script attached "SettingsController".
In this Script I have a function
public void GetPlanetCount(float sliderVal)
{
planetObjectsCount = Mathf.RoundToInt(sliderVal);
}
As you can see on the picture I have connected the method to the slider. But this method is never called.
Does someone know what is missing there?
I do not know much about the UI system at the moment

Its not getting called because you plugged in the script to the Object slot.
From your screenshot where it says "SettingsMenu (SettingsController)", you are suppose to plugin the SettingsMenu GameObject there not the SettingsController script.
Then on the right, you select the SettingsController script and the function.
Here is an image steps for that:
You can also do this from script. Look here.

Related

Detect controller input in VRTK

I'm kind of new to this so sorry if I'm writing in the wrong place - let me know and I'll move / delete this comment.
I'm currently having issues detecting controller input while using VRTK.
For example, when I have a collision between two objects, I want to be able to detect what buttons are being pressed on the controllers but can't seem to work out how I can do this.
Also, I have implemented the Interact Use functionality but I'm struggling to work out how to make two buttons do different actions.
For example:
once I grab an object with the simple pointer I want one button to
bring the object closer and another to move it away, but I've only
managed to implement one or the other.
Any suggestions? I've looked everywhere in the docs, examples and Google and can't seem to find anything. Any help would be MUCH appreciated! Pulling my hair out here!
You could utilise the Grabbedmethod on the InteractableObject: https://vrtoolkit.readme.io/docs/vrtk_interactableobject#section-grabbed-1
Or you could use the ControllerGrabInteractableObject event on The InteractGrab script: https://vrtoolkit.readme.io/docs/vrtk_interactgrab#section-class-events
Or you could have an Update routine and check the grabbed status on the controller doing GetGrabbedObject() != null (which checks to see if the controller has an object grabbed if it's null then one isn't grabbed).
Then you can use the ControllerEvents button bools to do something on a button press. So a script with this in that sits on the controller script alias gameobject next to the interact grab script:
void Update() {
if (GetComponent<VRTK_InteractGrab>().GetGrabbedObject != null) {
var controllerEvents = GetComponent<VRTK_ControllerEvents>();
if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Trigger_Press) {
//Do something on trigger press
}
if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Grip_Press) {
//Do something on grip press
}
}
}

Delay cmd -executeMethod or Set Active scene from cmd

I have BaseEmptyScene in Testproject1. BaseEmptyScene does not have any GameObjects. The only thing this project and scene has is a c# script with static method called bang().
I have a second Project called Sphere with Scene called 'Sphere'. It has basic GameObjects Sphere and Cube in it, no script. I went ' Assets > Export Package ' and exported everything to SphereCube.unitypackage
Here is my command line for executing method bang() after importing SphereCube.unitypackage into Testproject1.
C:\Program Files\Unity\Editor>Unity.exe -projectPath SomePath\TempProj -importPackage Path\SphereCube.unitypackage -executeMethod TestClass.bang
This commands opens unity , imports the package and execute the method bang fine. see unity command Line Argument doc
My problem is i cant tell unity to execute the method AFTER it finished importing the package. Here is what bang() looks like - currently it always displays scene count as 1. It should be 2 as i can see two scenes in Assets folder once the import is done (the empty scene with 1 script and the imported SphereCube scene)
private static void bang(){
...
sw.WriteLine("Scene Count = " + SceneManager.sceneCount);
sw.WriteLine("Active Scene = " + SceneManager.GetActiveScene().name + " " + SceneManager.GetActiveScene().path);
...
}
I cant even change the active scene to the newly added scene because as far as unity is concerned when the method is executed there is only 1 scene.
Is there some way i can execute the method once the import is done? Is there some event that raises....Something easier that getting the currently running Unity PID and sending that process a message from another external script...
What you need is the AssetPostprocessor class, instead of what you've done so far (side note: you don't need an empty scene for a static method somewhere in your project).
Most probably what you need is the OnPostprocessModel method (you can find sample code on this link as well).
As of your 'bang' method, you can do something as simple as this:
using UnityEngine;
public static class YourClass {
public static void Bang(GameObject myGO) {
//Do whatever you want with the GO you just imported
}
}
You don't need a scene or anything for this, as it's a static class with a static method; you can call Bang from whatever script you want (including your implementation of AssetPostprocessor)
Hope this helps!

Auto-reload Unity player (from scratch) after saving script?

I know Unity has an auto-refresh feature–but this doesn't restart the player from scratch. As far as I can tell, code in the Awake() or Start() hooks will not get run again after a script change.
I am building dynamic UIs based on web data for an online game, and I would like to be able to see changes automatically after saving a script. For now, I have to hit the play button manually.
Is this possible? Apologies if this is obvious. I'm new to Unity and have had a good look around the web!
You can do this in OnValidate() method of Monobehaviour.
Like this:
void OnValidate()
{
// do your stuff here.
}
OR
you can use ExecuteInEditMode attribute on your script.
Like this:
[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
if (target)
transform.LookAt(target);
}
}

Selecting a GameObject in Unity Editor from MonoDevelop Debugger

I'm debugging a MonoBehaviour script in MonoDevelop.
There is a way to select (in the Unity Editor) the gameobject the current paused script is attached to?
(I have multiple instances of the same prefab with that script attached, so finding it in the Hierarchy is not trivial)
You can do this fairly easily by taking advantage of Unity's Selection class. Just be sure to add using UnityEditor; at the top of your script.
To select the gameObject you are debugging in your hierarchy, simply set the Selection.activeGameObject property in your script right after the line you are placing your break point on. For example:
void Update()
{
int breakPoint = 5; //your breakpoint is placed here
//select this gameObject in the hierarchy
Selection.activeGameObject = this.gameObject;
}

Unity GUI Text won't work

I have the following problem, and I'd appreciate greatly if someone could help.
I've been trying create a roll-a-ball game in Unity, similar as the one presented in one of the tutorials on their official website, but with one difference: I'd like to add GUI text that would show the score. Every time the ball hits (and destroys) one of the rotating cubes, the score should increase by 1, and the text should show the current score.
I've watched the following tutorial:
http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points
I have created the UI text, called ScoreText, and, on the other hand, I have written the script that makes an instance of GUIText class, called guitext, show the current score. However, I cannot connect my particular GUI text (ScoreText) with the script, i.e. I cannot tell the script to use that particular GUI text to show score. In the video, at 16:35, they just drag the GUI text to the instance of the GUIText class from the script (in my case, guitext), in order to tell the script it's that particular text that should show the score. But when I try to drag it, I simply can't do it. I have no idea why.
I don't know if this could cause the problem: under "Create" in the free version of Unity, I could not find anything called "GUI text" but instead I created a UI text. I know it's the same thing, but... In the script, I define an instance of GUIText class - guitext - so maybe, when I try to add my UI text to it, it won't work because my text really doesn't belong to the GUIText class?
Since you are using Unity 4.6 which has a new GUI system and not the Legacy UI which was present when the tutorial which you mentioned was made. There are two options - either you create an empty gameobject and add add a GUIText component to it. You can add the GUIText to empty gameobject by selecting it in the Hierarchy which should display the objects properties in the Inspector panel.
In the Inspector, you should see an Add Component button, click on it and search for GUIText. Add it and that should solve your problem.
Oh yea, the second option is to migrate to Unity 4.6 new GUI, which should take time unless you are pretty good with unity.
If you want to learn Unity 4.6 GUI, you can refer these links
Unity Official
Unity 4.6 tutorials by TheGameContriver
I am only posting on this because many come to this one and get lost. Here is how to make this work in Unity5.3 at the time of this response.
This is in C#
Simply use the GameController game object to attach the script to. Second create a UI text. Toy only call it score or scoretext for your own referral otherwise the name itself does not matter.
using UnityEngine;
using UnityEngine.UI;///needed to reference the new UI setup in this script type
using System.Collections;/// <summary>
/// allows collections of related information.
/// </summary>
public class GameController : MonoBehaviour {
public GameObject targets;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text Text; /// <summary>
/// change this because you dont actually use the words UI or GUI. Thats just for the reference and not clarified by instructors.
/// </summary>
private int score;
void Start()
{
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
IEnumerator SpawnWaves()///this depends on the above using System.Collections
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(targets, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
Text.text = "Score: " + score;
}
}
Confirmed. In the GameController.cs file, change the declaration of public GUIText scoreText to public Text scoreText, and then it will let you drag it on the Game Controller as the video shows.
Update your Unity tutorials, peoples! Just spent an hour trying to figure this out!
One more thing...check the Z position. My UI looked perfect head on in the scene view, but my text never showed in the game...then I rotated the scene view and found my text was way behind the camera. Seems when you add something to the UI, the z value is somewhat random.

Resources