- Home /
How to access the properties of any created Game Object from another script Unity C # 2D
I have instantiated multiple game objects like this part of the code:
GameObject newCard = Instantiate(CardPrefab, new Vector3(x, y, z), transform.rotation); x,y and z being parameter so I could spawn the objects in different positions.
My question is how could I access the name of the Game objects or the properties from another script. Each of my game objects has a property which gives it a value.
In another script I want to add the values of all objects but I am not sure how to access the objects from another script to get their name and values.
Thanks for the support.
Answer by bennett_apps · Aug 02, 2018 at 07:10 PM
You can reference an instantiated GameObject the same ways you reference any other GameObject...except you can't assign it in the inspector before pressing play as it is instantiated at runtime...you can take your newCard variable and use GetComponent, etc. to grab what you need. If you make your newCard variable public you could reference the variable from another script.
Your answer
Follow this Question
Related Questions
Camera switch between child back to main camera issue 2 Answers
cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers
How to debug the object position in the grid of objects? 0 Answers
How to store a gameobject in a variable 1 Answer
Cloned/Instantiated GameObject's Losing Public Variables When Created? 1 Answer