How to get an object (that is a prefab) from an array and instantiate it (C#)
Hi, I'm working on a game, and I'm making a building system. Instead of having 9 variables, I want to use an array. The Script:
public Object[] buildables;
public Transform previewOfWhereItsPlaced;
private Object buildBeingBuilt;
Then, in update, I wrote:
//code that senses if Alpha3 is pressed
buildBeingBuilt = buildables.GetValue (1);
//then it does more code
But it says you can't implicitly convert type 'object' to 'UnityEngine.Object' and something about casting. Even when I type instantiate correctly, it still does this, because the error is on the line with the private variable getting the value from the Array.
Answer by Hellium · Nov 24, 2017 at 05:16 PM
public GameObject[] buildables; // Drag & Drop the prefabs
public Transform previewOfWhereItsPlaced;
private GameObject buildBeingBuilt; // Will hold a reference to the instance of the prefab
// Update function
//code that senses if Alpha3 is pressed
buildBeingBuilt = Instantiate( buildables[1] ) ;
//then it does more code
Your answer
Follow this Question
Related Questions
Json to array of objects C# 1 Answer
[help] Array index is out of range 0 Answers
how to start counting from the last index the script stop on? 2 Answers
Enemy AI. How to make the AI chase the player by using the exact same moves as the player. (2D) 1 Answer
Fill new array with an old array every time two agents collide 0 Answers