Delay cmd -executeMethod or Set Active scene from cmd - unityscript

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!

Related

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.

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

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

Turning an avatar into a 3rd person character

I just learned how easy it is to simply drag a 3rd person character controller prefab (from Unity's standard assets package) and drop it into the hierarchy.
Using the WSAD and Space keys feels pretty natural, so I wondered if I could apply the same character controller to a customized avatar.
Using the free AutoDesk Character Generator (https://charactergenerator.autodesk.com/) I created one (fbx file) and imported it into Unity, so now I have my own character prefab.
I, then, searched for the steps to animate it just like a a 3rd person character controller, with the following article coming up first, but I wonder if I always have to do all the steps?
http://blogs.unity3d.com/2014/04/14/turn-your-character-into-a-player/
Once you have a customized character in the form of a Unity prefab, should do still go through all these steps, or is there a simpler way of animating your avatar; e.g. adding the basic necessary scripts?
That article gives a good overview of what is required.
However, you can essentially skip almost all those steps when using autodesk character generator.
Here is a quick way:
Set your FBX to be a mecanim humanoid
Drag an mecanim animator controller onto it
Write your code to set the animator states (eg: speed, jumping, etc)
To do that:
Export from Autodesk as "Unity FBX" format to get YourCharacter_Unity.fbx
Drag the FBX into the unity project files
Click on the YourCharacter_Unity FBX in project (blue cube), select "RIG" tab in inspector, and change Animation type to "Humanoid" (which maps it to the Mecanim system).
Drag the FBX from the project into the scene.
Go to the Asset Store and import "Mecanim Locomotion Starter Kit" (which has a basic locomotion controller and a set of animations)
Drag the "Locomotion Setup/Locomotion/Locomotion.controller" onto the "controller" variable on your character's Animator component.
Untick "apply root motion"
Now, if you hit run you will see your character standing there with idle motions. If you double click on the animator controller on your character it will open up the Mecanim Animtor window and you can manually set the animation state. Try changing the speed to 1.0 and you will see him walk/run.
Note: In your character's Animator component if you checkmark "Apply root motion", then the feet animation will cause your avatar to automatically move when his speed > 0.
You say you are using a CharacterController, so here is a very simple script that references the character controller to get the current speed, and then set the speed on the Animator:
using UnityEngine;
using System.Collections;
public class CharacterAnimator : MonoBehaviour {
public CharacterController controller;
public Animator animator;
private int speedid;
void Start () {
animator = GetComponentInChildren<Animator>();
controller = GetComponent<CharacterController>();
speedid = Animator.StringToHash("Speed");
}
void Update () {
float speed = controller.velocity.magnitude;
animator.SetFloat(speedid, speed);
}
}

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