- Home /
Convert String to GameObject[] type Array
I have found similar questions but they dont seem to answer my exact question.
What i am trying to do is set a newly created GameObject[] equal to a GameObject[] that has been filled before runtime manually while using a String to connect the names.
Here is what i mean,
var highlightsNight : GameObject[]; //this is filled manually in editor with what i need to highlight var timeOfDay : String = "Night"; //there can be more than one time of day, changes dynamically from a string comparison elsewhere
function turnOnHighlights() { var currentHighlights : GameObject[] = ("highlights"+timeOfDay); //need to set this by String name equal to the highlights for that time of day }
Thanks for your help! I hope this is an easy one, i just cant seem to get around the error involving Cant Convert String into GameObject.
EDIT, Sorry for the confusion. ----------------------------------------------------
I will try to make this more clear!
I have multiple GameObject[]'s declared inside of a script called waypoints. When you attach the waypoints script to something, an empty GameObject for example, you can then, in the editor manually define the length and add game objects by dragging and dropping them into this array list area created in the scripts component area on the GameObject that this is all happening on.
In this same script i also have to create an empty GameObject[] on the fly. Then set this new GameObject[] equal to one of the ones i previously declared and explained in the pervious paragraph. How can i tell the newly created GameObject[] to be equal to one of the perviously created ones by used of a string? When i try this it doesn't work.
var highlight_i2 : GameObject[]; // i will fill this up in the editor with objects i want to highlight during step i2 var thisStepName : String = "i2"; //this is a constaly changing string which i use to direct where we are in the scene
function turnOnHighlights() //this gets called every step in the scene to change what is highlighting
{
var highlightObjects : GameObject[];
highlightObjects = GameObject.Find("highlight_"+thisStepName); //This is what i cant figure out
// do you see what im trying to do here? //all of my highlight arrays are named "highlight_(stepName)" like the one above //goes on to highlight these objects in highlightObjects }
and i get the following Error:
Assets/Scripts/waypoints.js(318,51): BCE0022: Cannot convert 'UnityEngine.GameObject' to '(UnityEngine.GameObject)'.
Your question makes little sense, I'm afraid. The code you post attempts to concatenate a string with an array of gameObjects, something that the compiler will barf at. What exactly are you trying to accomplish? To simply change the name of each gameObject, or something like pre-filter the list of gameoObjects based on a tag or name pattern?
Do you want an array of strings? cos that's just String[]
Answer by _Petroz · Jul 01, 2010 at 11:49 PM
If you're trying to lookup a GameObject by name you should use GameObject.Find
http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
Answer by Tetrad · Jul 02, 2010 at 01:47 AM
There isn't a function to find all the game objects with a given name.
If you use tags, though, you can use GameObject.FindGameObjectsWithTag
Your answer
Follow this Question
Related Questions
convert GameObject to Rigidbody type 2 Answers
Array - Convert Object into Int 5 Answers
String Parsing 1 Answer
How to convert a string to int array in Unity C# 1 Answer
Converting String to a Game Object 1 Answer