Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UnityEngine.GameObject being added to my string

Discussion in 'Scripting' started by Wuggie88, Nov 27, 2020.

  1. Wuggie88

    Wuggie88

    Joined:
    Sep 17, 2019
    Posts:
    9
    Hey there.


    I'm having some trouble with unityEngine.GameObject being added to my string when i try to make it, the way the string is made is from getting an item out from another script, but due to this being in an array, it adds this line to it for some reason (at least that's what my testing so far have given me)

    i have tried to hardcode pull something out of the array but it does the same thing.
    if i only pull the "s" (the number) it works without adding anything behind it.

    i have googled around but i can't find anyone else with this problem.

    Code (CSharp):
    1. circuitName = circuitSpawner.GetComponent<Spawner>().prefabs[circuitSpawner.GetComponent<Spawner>().s].ToString();

    This is how it looks:
    upload_2020-11-27_9-41-7.png

    I'm sorry if this is a stupid question but i really need it to not write the unityengine.gameobject

    (i have also tried without a .ToString and simply set it directly without a ToString, but the same thing happens, this is simply where I couldn't think of any more solutions and the best way to display it)


    What this is for:
    The game has more spawners and more of these prefabs, but i need to target whatever one is spawned, i can't do this by a tag, since several will be spawned, throughout the map and each button (which this script is on) need to target a specific one. (i hope this makes sense)
     
  2. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    If its a GameObject then it must have a name, so instead of using ToString() you can just use that parameter
    Code (CSharp):
    1. circuitName = circuitSpawner.GetComponent<Spawner>().prefabs[circuitSpawner.GetComponent<Spawner>().s].name;
    .
     
    Wuggie88 likes this.
  3. Wuggie88

    Wuggie88

    Joined:
    Sep 17, 2019
    Posts:
    9

    I cannot tell you how much I feel like an idiot right now, think I've been at it for too long in a row by now.. Thanks, that does work! I ended up writing a split of the string and the using the first part of the split.. but this is def the better way to do it..