I purchased an asset for Space Flight Controls. Im trying to modify one of the scripts to display the public speed variable. I’ve followed a few tutorials for displaying text and tried numerous ways to incorporate the code into my flight control script with no luck. I don’t want to post the script because it came from a purchased asset. I’ve tried to write a separate script to get the speed and display it but that doesn’t work either. Can anyone tell me what Im doing wrong?
How do you wish to display the speed?
Via a UI element such as a UI Text?
Yes via UI element. I have one anchored in position and I have a flight control script that controls the speed on my spaceship.
If you don’t mind, could you post the name of the asset, or better yet, an asset store link to the asset you are working with?
Some of us might already have the asset, and would be able to look into the code you are referring too.
I am assuming that the speed variable is actually public, or you have made it public, and that it is a float.
Given this, all you should have to do is:
// The text UI element used to display the speed
Text speedDisplay;
// The ship script that has the public speed variable.
ShipScript ship;
void Update(){
speedDisplay.text = ship.speed.ToString();
}
Make sure to have:
using UnityEngine.UI;
At the top of your script, so that you can access the UI code, such as Text.
Here is the link to the asset.
https://www.assetstore.unity3d.com/en/#!/content/24532
Do I create a new Csharp script with your code or do I add it to the flight control script Im using?
You can do either.
Although, I do suggest making your own, simply because when the asset updates, you will have to modify it all over again, which you can avoid by just making your own script that accesses the needed information.
Okay so I made my own but Im getting an error. I copied and pasted the script below. Sorry Im sure theres a better way. The console gives me a red error message on the script.
Assets/Scripts/DisplaySpeed.cs(8,9): error CS0246: The type or namespace name `Text’ could not be found. Are you missing a using directive or an assembly reference?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplaySpeed : MonoBehaviour
{
//ThetextUIelementusedtodisplaythe speed
Text speedDisplay;
//Theshipscriptthathasthepublicspeedvariable.
ShipScript ship;
void Update(){
speedDisplay.text = ship.speed.ToString();
}
}
Oka Ive got it working however it doesn’t work when I use my thruster to increase the speed. The flight control script has a base speed and an afterburner speed. It only displays the base speed and doesn’t update.
Sounds to me what you want is the ship’s total current speed, right?
Thank you for your help. I added the script to the text UI element. Should I drag it to the ship maybe? It doesn’t update the speed when the afterburner is used it only show the base speed of the ship.
Yes kind of. The ship has a brake that slows the speed to a float set at 3. The ship has a base speed which is float 7 and there is an afterburner that works when keydown that increases speed to 40 gradually. I want the ships speed no matter what it is displayed whether braking or thrusting or cruising.
Does the ship have a Rigidbody?
In other words, does the shipscript use Physics to move the ship?
Yes
Then what you could do, is grab the velocity of the ship’s Rigidbody, instead of grabbing the ship’s speed.
Here is what I tried: speedText.text = ship.rigidbody.velocity.ToString();
velocity isn’t part of Rigidbody though.
Well no, not “Rigidbody”, but it is a data member of the Rigidbody component, which is what I meant, sorry.
That said, does it work using the velocity?
You said you tried it, but you did not say if it worked.
No it did not work. Mono changed .velocity.ToString font red.
I believe I need to declare the rigidbody and somehow use get component but I can’t seem to get anything going Im learning. Here is what I have. I changed from Update to FixedUpdate after some reading but not sure if thats correct either.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplaySpeed : MonoBehaviour
{
// The text UI element used to display the speed
public Text speedText;
// The ship script that has the public speed variable.
public PlayerFlightControl ship;
//The rigidbody of the ship to get the constant velocity
public Rigidbody rigd;
void FixedUpdate()
{
speedText.text = ship.rigidbody.velocity.ToString();
}
}
The beautiful MonoDevelop altered the script and even asked me If I had a backup first. This after I made some changes to the script since I posted moments ago. Now it works but I have some zeros and decimals in front of the speed. How do I ignore the decimals? Heres the new script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplaySpeed : MonoBehaviour
{
// The text UI element used to display the speed
public Text speedText;
// The ship script that has the public speed variable.
public PlayerFlightControl ship;
//The rigidbody of the ship to get the constant velocity
public Rigidbody rigd;
void FixedUpdate()
{
speedText.text = ship.GetComponent<Rigidbody>().velocity.ToString();
}
}