1
\$\begingroup\$

I have this line of code

void Update () { float alVal = 0.0f; float damping = 1f; alVal += damping * Time.time; GameObject text2 = new GameObject(); text2.AddComponent (typeof(GUIText)); text2.guiText.color = new Color (1f,1f,1f); text2.guiText.name = "name"; text2.guiText.text = "YOLOMF"; text2.guiText.pixelOffset = new Vector2 (100, 100); text2.guiText.color = new Color(1f,1f,1f,alVal); Debug.Log (alVal); } 

I want to create a SINGLE TEXT, but I guess the Update() function keeps on making more.

I need that SINGLE TEXT to do an animation of Fade-In that's why it is in Update()

\$\endgroup\$
5
  • \$\begingroup\$ I guess you'd create the GUIText outside of Update then, so it only gets created once, then animate it inside Update? Or set and check a flag to make sure it happens only once? This sounds like it's more about programming and not really about game development. \$\endgroup\$ Commented Dec 29, 2014 at 3:10
  • \$\begingroup\$ It's not programming really, I Just want to create a GUI Text using a code, but when I declare it outside the update, Update() function can't use it so that's why I placed the declaration inside the Update(). I cannot use flag because flag stops at 1 frame, but the Fade-In happens in a lot of frames. \$\endgroup\$ Commented Dec 29, 2014 at 3:23
  • \$\begingroup\$ You definitely should be able to use an object declared outside of a method inside a method. That seems like the root problem. I recommend making a minimal example of what you're trying and asking on StackOverflow, who are a programming-focused site. \$\endgroup\$ Commented Dec 29, 2014 at 3:28
  • \$\begingroup\$ I would do your recommendation. If I may ask sir, would you make a code block wherein you can create a single GUIText in C and do a fade-in/fade-out? I think that would help a lot and might also solve my current problem. Thank you sir @Anko. \$\endgroup\$ Commented Dec 29, 2014 at 3:48
  • 1
    \$\begingroup\$ Sorry, I don't have the time to go into more detail right now, but I hope you figure it out! Friendly tip: To get the best answers, it's best to just concentrate on describing the problem, rather than the problem with a solution to that problem. \$\endgroup\$ Commented Dec 29, 2014 at 3:58

1 Answer 1

2
\$\begingroup\$

This is a rough translation of the code you've provided into something that does not spawn a new GameObject on every frame.

private GUIText textObject; void Start () { // Initialize the text when the behavior is "started" GameObject text2 = new GameObject(); textObject = text2.AddComponent<GUIText>(); textObject.color = new Color (1f,1f,1f); textObject.name = "name"; textObject.text = "YOLOMF"; textObject.pixelOffset = new Vector2 (100, 100); } void Update () { // Fade a bit every frame ... float alVal = 0.0f; float damping = 1f; alVal += damping * Time.time; textObject.color = new Color(1f,1f,1f,alVal); // TODO: Re-architect this to clean itself up. } 

There's several ways to improve the architecture of this but the "best" way completely depends on other architecture not defined here. This will have the effect you're looking for but I'd recommend you alter it to clean up after itself.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.