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

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);
}
}

Related

Problem with colliding a player with a trigger and making the player go back to the main menu

I made a trigger where the player collides and it goes to the main menu (scene 0) and it just wont work. I'm using unity with c#:
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneTransition : MonoBehaviour
{
public string SceneToLoad;
public void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) ;
{
SceneManager.LoadScene(SceneToLoad);
}
}
}
`
There is very little detail in your question. Please check the following:
1. Is the collider attached to both the player and the trigger object?
2. What are their settings? Kinematic,etc ?
3. do you have a RigidBody attached to the player or the trigger object?
4. Is the SceneToLoad already registered in the Build?
These are the basics to make what you want. I suggest you revise the following topics:
1. Collision, Colliders and Rigidbody, especially search for Collision matrix
2. How SceneManagement works
Is your game 3D or 2D? If its 3D, change the OnTriggerEnter2D to JUST OnTriggerEnter().
And check that the Is Trigger in the box collider (or whatever collider you're using) is checked. :)
You also put in a semicolon in the if(other...) so remove that. Semicolons aren't supposed to be put in to if statements

Unity Slider does not return a value in the Script

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.

deal with Unity 5 UI in augmented reality app

I'm trying to make an augmented reality application with vuforia and unity.
whenever it recognize the image target, it must tell a story by showing text , and it should enable the user to press next and back to go on reading the different parts of this story, I'm totally new to unity and don't know how to handle with UI throughout scripting, I need some help on how to accomplish the part of "going forward and backward on showing the story by hitting Next and Back buttons", and all these parts of story should be related to the same image target in the same scene.
I appreciate it if you help me with an example code.
You should create some script that attach on trackable object, maybe something like this.
public class DataBook {
string[] dataBook;
string idText;
bool isActive;
}
Then you must create another script to set that trackable object is active or not, this link can help you to get that.
https://developer.vuforia.com/forum/faq/unity-how-do-i-get-list-active-trackables
Then after you get the active trackable object, you can set the dialog from the book by create another controller script for button, example
public void Next() {
DataBook[] books = FindObjectsOfType<DataBook>(); // if the object more than one, it will be more easy if it only the one
foreach (var book in books)
{
if (book.isActive) {
book.idText += 1;
textUI.text = book.dataBook[idText]; //textUI assign to object text on canvas
}
}
}
you can learn about unity UI Button on this :
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Good luck

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.

Coded UI Test without Element Detection

Is there any way to create a coded UI test for a WPF application that only detects the parent window element? We are using a component suite that does not support Coded UI tests, but I would still like to be able to automate the UI for testing purposes. Ideally, such a solution would detect the parent window element, then use pixel offsets for automating any button presses, etc.
Thanks.
You can use the Coded UI Test Builder to discover the properties you need, but the basic method for getting a window is pretty simple.
Here's a really simple class representing a window:
using System;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WpfControls;
public class MyWpfWindow : WpfWindow
{
public MyWpfWindow() : base()
{
this.SearchProperties[WpfWindow.PropertyNames.Name] = "My Wpf Window Name";
}
}
And here's a test
[CodedUITest]
public class MyWpfWindowUITests
{
[TestMethod]
public void MyWpfWindowExistsTest()
{
Assert.IsTrue(MyWpfWindow.Exists(5000), "The window was not found within five seconds");
}
}
While technically it is possible (Mouse.Click has overloaded version with x,y coordinates), it is not a good idea. Any change in control layout will break your test.
http://social.msdn.microsoft.com/Forums/en-US/a2c585ee-8db0-48c0-8825-fb1865631977/how-do-i-click-checkbox-inside-drawhighlight-method
This link has the code to click on the coordinates within a defined highlighted area

Resources