c# - Gui Text issue not appearing -
i'm having problem gui text in unity 5. trying score appear on screen. scoring system works since have debug.log output tells me works, gui text isn't showing if type text box.
using unityengine; using system.collections; public class score : monobehaviour { public static int myscore; public bool allowscore = false; public guitext mytext; //only allows score start when first object has passed player object void ontriggerenter2d(collider2d collisionobject) { allowscore = true; } //add 1 score every switch void update () { if (input.getmousebuttondown (0) && allowscore == true) { myscore = myscore + 1; debug.log ("my score " + myscore + " point(s)"); } //gui text screen mytext.text = "score: " + myscore.tostring(); } }
unless typo, have line of code has 2 equal signs:
mytext.text = "score: " = myscore.tostring();
what guessing trying have score: , display score. change second equal plus.
mytext.text = "score: " + myscore.tostring();
also 1 helpful hint, on line of code says myscore = myscore + 1;
, can myscore++;
, , if wish add more 1 @ somepoint can myscore += amounttoadd
. +=
means want equal plus other side of equation, ++
means add 1 it.
edit: response comment
nullreferenceexceptions occur when try assigning value variable un-assigned in system. , seeing above, in code declare variable public guitext mytext
, later try altering objects text, however, in code mytext
equal to. when calling mytext.text
trying set text of non existent(null) variable. fix this, in unity once have exited code, in inspector able see code attached object. in script component see variable mytext
, drag gameobject guitext component hierarchy slot.
Comments
Post a Comment