Search Unity

Help Wanted, Trying to code floating text in a 2D rpg game.

Discussion in 'Scripting' started by Zor, Jun 9, 2021.

  1. Zor

    Zor

    Joined:
    Aug 30, 2013
    Posts:
    2
    I've been following a tutorial and basically copying the code from it (I'm new to unity and have very basic knowledge in coding). I've checked the video at least 5 times and can't seem to find any mistakes.

    I'm getting the following error message: Assets/Scripts/FloatingTextManager.cs(46,42): error CS1001: Identifier expected.
    The line that's giving me trouble is: txt.go Instantiate(textPrefab);
    When I remove it I stop getting the error message but the floating text doesn't work.

    This is the whole script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class FloatingTextManager : MonoBehaviour
    8. {
    9.     public GameObject textContainer;
    10.     public GameObject textPrefab;
    11.  
    12.     private List<FloatingText> floatingTexts = new List<FloatingText>();
    13.  
    14.     private void Update()
    15.     {
    16.         foreach(FloatingText txt in floatingTexts)
    17.             txt.UpdateFloatingText();
    18.  
    19.     }
    20.  
    21.     public void Show(string msg, int fontSize, Color color, Vector3 position, Vector3 motion, float duration)
    22.     {
    23.         FloatingText floatingText = GetFloatingText();
    24.  
    25.         floatingText.txt.text = msg;
    26.         floatingText.txt.fontSize = fontSize;
    27.         floatingText.txt.color = color;
    28.      
    29.         floatingText.go.transform.position = Camera.main.WorldToScreenPoint(position);
    30.         floatingText.motion = motion;
    31.         floatingText.duration = duration;
    32.  
    33.         floatingText.Show();
    34.  
    35.  
    36.  
    37.     }
    38.  
    39.     private FloatingText GetFloatingText()
    40.     {
    41.         FloatingText txt = floatingTexts.Find(t => !t.active);
    42.  
    43.         if(txt == null)
    44.         {
    45.             txt = new FloatingText();
    46.             txt.go Instantiate(textPrefab);
    47.             txt.go.transform.SetParent(textContainer.transform);
    48.             txt.txt = txt.go.GetComponent<Text>();
    49.  
    50.             floatingTexts.Add(txt);
    51.  
    52.         }
    53.  
    54.         return txt;
    55.     }
    56. }
    57.  
    Any help would be highly appreciated, thanks in advance!
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Code (csharp):
    1.  
    2.           txt.go Instantiate(textPrefab);
    3.  
    should probably be
    Code (csharp):
    1.  
    2.           txt.go = Instantiate(textPrefab);
    3.  
    You are missing an equal sign. This is the assignment operator which puts the value returned by Instantiate in the variable.

    So you waited almost 8 years to make your first post? Thats patience ;).

    If you are having issues with basic C# and syntax I recommend the free C# Yellow Book by Rob Miles.
     
    Zor and Kurt-Dekker like this.
  3. Zor

    Zor

    Joined:
    Aug 30, 2013
    Posts:
    2
    Ohhh, thank you so much! It works perfectly!
    Yeah, when I started using unity 8 years ago I gave up pretty fast and went into graphic design professionally, but I would really love to get into amateur game design as a hobby, maybe even as a career option a few years down the line.
    Thanks so much for the help and the book recommendation, I'll be sure to read through it. Cheers!
     
    exiguous likes this.