Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bubblegumsoldier · May 04, 2011 at 02:43 PM · gameobjectvariableguitextdisplay

how can I display a variable as a GUIText

Hi Guys,

Can anybody of you tell me how I can display a variable of a Script as a GUIText

that I've added in the scene view?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by ellens · Sep 05, 2012 at 12:35 PM

This will work:

 var myNiceVariable = "Here I am";
 var guiText = GameObject.Find("GUI Text").GetComponent(GUIText);
 guiText.text = myNiceVariable;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Aldwoni_legacy · May 04, 2011 at 02:49 PM

guiText.text = variable.toString();

http://unity3d.com/support/documentation/ScriptReference/GUIText-text.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

Answer by bubblegumsoldier · May 05, 2011 at 12:33 PM

PLEASE HELP ME!!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by bubblegumsoldier · May 04, 2011 at 02:59 PM

but the variable is in another script... can I just say

GUIText.text = nameofscript.variableofscript.toString();

because it doesn't work:

My error:

Assets/WeaponScripts/FPSPlayer.js(112,49): BCE0020: An instance of type 'm4a1script' is required to access non static member 'bulletsLeft'.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aldwoni_legacy · May 04, 2011 at 03:03 PM 0
Share

you could better use comments for reply's on an answer. if a variable is public then it should be useable in other scripts. do you script in C# or Javascript or Boo?

avatar image bubblegumsoldier · May 04, 2011 at 03:05 PM 0
Share

ohhh you mean I have to set it to public?!!!

Oh man! I always had it as a privat variable!

avatar image Aldwoni_legacy · May 04, 2011 at 03:06 PM 0
Share

then you need to make bulletsLeft static. public static var bulletsLeft : int; // for Javascript public static int bulletsLeft; // for C#

avatar image Aldwoni_legacy · May 04, 2011 at 03:10 PM 0
Share

a variable in javascript is public by default.

avatar image Aldwoni_legacy · May 04, 2011 at 03:11 PM 0
Share

" public var bulletsLeft : int = 0;" needs to be " public static var bulletsLeft : int = 0;"

Show more comments
avatar image
0

Answer by bubblegumsoldier · May 04, 2011 at 03:32 PM

I'll write the full two scripts here:

  1. FPSPlayer:

var maximumHitPoints = 100.0; var hitPoints = 100.0;

var bulletGUI : GUIText; var MagazineGUI :GUIText; var rocketGUI : DrawRockets; var healthGUI : GUITexture; var bulletm4a1GUI :GUIText; var walkSounds : AudioClip[]; var painLittle : AudioClip; var painBig : AudioClip; var die : AudioClip; var audioStepLength = 0.3;

private var machineGun : MachineGun; private var MMM : m4a1script; private var rocketLauncher : RocketLauncher; private var healthGUIWidth = 0.0; private var gotHitTimer = -1.0;

var rocketTextures : Texture[];

function Awake () { machineGun = GetComponentInChildren(MachineGun);

rocketLauncher = GetComponentInChildren(RocketLauncher);

PlayStepSounds();

healthGUIWidth = healthGUI.pixelInset.width;

}

function ApplyDamage (damage : float) { if (hitPoints < 0.0) return;

// Apply damage hitPoints -= damage;

// Play pain sound when getting hit - but don't play so often if (Time.time > gotHitTimer && painBig && painLittle) { // Play a big pain sound if (hitPoints < maximumHitPoints 0.2 || damage > 20) { audio.PlayOneShot(painBig, 1.0 / audio.volume); gotHitTimer = Time.time + Random.Range(painBig.length 2,

painBig.length 3); } else { // Play a small pain sound audio.PlayOneShot(painLittle, 1.0 / audio.volume); gotHitTimer = Time.time + Random.Range(painLittle.length 2, painLittle.length * 3); } }

// Are we dead?
if (hitPoints < 0.0)
    Die();

}

function Die () { if (die) AudioSource.PlayClipAtPoint(die, transform.position);

// Disable all script behaviours (Essentially deactivating player control) var coms : Component[] = GetComponentsInChildren(MonoBehaviour); for (var b in coms) { var p : MonoBehaviour = b as MonoBehaviour; if (p) p.enabled = false; }

LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);

}

function LateUpdate () { // Update gui every frame // We do this in late update to make sure machine guns etc. were already executed UpdateGUI(); }

function PlayStepSounds () { var controller : CharacterController = GetComponent(CharacterController);

while (true) {
    if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
        audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
        audio.Play();
        yield WaitForSeconds(audioStepLength);
    } else {
        yield;
    }
}

}

function UpdateGUI () { // Update health gui // The health gui is rendered using a overlay texture which is scaled down based on health // - Calculate fraction of how much health we have left (0...1) var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * 

healthFraction;

// Update machine gun gui // Machine gun gui is simply drawn with a bullet counter text if (machineGun) { bulletGUI.text = machineGun.GetBulletsLeft().ToString();

}

if(MMM){

 bulletm4a1GUI.text = m4a1script.bulletsLeft.toString();

} // Update rocket gui // This is changed from the tutorial PDF. You need to assign the 20 Rocket

textures found in the GUI/Rockets folder // to the RocketTextures property. if (rocketLauncher) { rocketGUI.UpdateRockets(rocketLauncher.ammoCount); /if (rocketTextures.Length == 0) { Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property."); } else { rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount]; }/ } }

________________ ________________

and the second code:

var range = 100.0; var fireRate = 0.05; var force = 10.0; var damage = 5.0; var bulletsPerClip = 40; var clips = 20; var reloadTime = 0.5; private var hitParticles : ParticleEmitter; var muzzleFlash : Renderer; var Huelse :Transform; var bulletsLeft = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1; var spawnpoint :Transform;

function Start () { hitParticles = GetComponentInChildren(ParticleEmitter);

// We don't want to emit particles all the time, only when we

hit something. if (hitParticles) hitParticles.emit = false; bulletsLeft = bulletsPerClip; MagazineLeft = clips; }

function LateUpdate() { if (muzzleFlash) { animation.Play("shotmachine"); var Huelseestellen = Instantiate(Huelse, transform.position, Quaternion.identity);

Huelseestellen.rigidbody.AddForce(transform.up*10);

         // We shot this frame, enable the muzzle flash
 if (m_LastFrameShot == Time.frameCount) {
     muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
     muzzleFlash.enabled = true;

     if (audio) {
         if (!audio.isPlaying)
             audio.Play();
         audio.loop = true;
     }
 } else {
 // We didn't, disable the muzzle flash
     muzzleFlash.enabled = false;
     enabled = false;

     // Play sound
     if (audio)
     {
         audio.loop = false;
     }
 }

}

}

function Fire () { if (bulletsLeft == 0) return;

// If there is more than one bullet between the last and this frame // Reset the nextFireTime if (Time.time - fireRate > nextFireTime) nextFireTime = Time.time - Time.deltaTime;

// Keep firing until we used up the fire time while( nextFireTime < Time.time && bulletsLeft != 0) { FireOneShot(); nextFireTime += fireRate; }

}

function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit;

// Did we hit anything? if (Physics.Raycast (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

 // Place the particle system for spawing out of place where we hit the

surface! // And spawn a couple of particles if (hitParticles) { hitParticles.transform.position = hit.point; hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.right, hit.normal); hitParticles.Emit(); }

    // Send a damage message to the hit object          
    hit.collider.SendMessageUpwards("ApplyDamage", damage,

SendMessageOptions.DontRequireReceiver); }

bulletsLeft--;

// Register that we shot this frame, // so that the LateUpdate function enabled the muzzleflash renderer for one frame m_LastFrameShot = Time.frameCount; enabled = true;

// Reload gun in reload Time

} function Reload () {

// Wait for reload time first - then add more bullets! yield WaitForSeconds(reloadTime);

// We have a clip left reload if (clips > 0) { clips--; bulletsLeft = bulletsPerClip; MagazineLeft = clips; }

} function bulletsdisplay() {

} function Update(){

if(Input.GetAxis("Reload") == 1){ //if(Input.GetButtonDown("Reload")){

 Reload();

 }   

}

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aldwoni_legacy · May 04, 2011 at 03:34 PM 0
Share

why you use UpdateGUI ins$$anonymous$$d of OnGUI?

avatar image bubblegumsoldier · May 04, 2011 at 03:35 PM 0
Share

well it was already there... half of th code wasn't made by myself

avatar image bubblegumsoldier · May 04, 2011 at 03:35 PM 0
Share

should I try it?

avatar image bubblegumsoldier · May 04, 2011 at 03:37 PM 0
Share

well it doesn't change anything

avatar image bubblegumsoldier · May 04, 2011 at 04:01 PM 0
Share

PLEASE HELP $$anonymous$$E!!!!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers

How can I access a variable from another script in a separate object? 1 Answer

Problem Displaying Score on GUI text 0 Answers

How to access gameObject variable script 2 Answers

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges