I’m not sure how to get the second prefab to know where to instantiate. The problem is that the first prefab doesn’t exist when the scene starts, until it’s placed by the player. So I’m not sure how to refer to an object position/rotation that isn’t in the scene yet.
Here is how the initial seed gets planted, which works fine:
public Transform placePoint;
//placePoint is an empty GameObject on the player that indicates where a seed should be planted
public GameObject seedPrefab;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Plant();
}
}
void Plant (){
//planting logic
Instantiate(seedPrefab, placePoint.position, placePoint.rotation);
}
The problem is turning the seed into a plant in another script:
public GameObject seed;
public GameObject seedPrefab;
public GameObject plantPrefab;
void Update()
{
Invoke("Grow", 2);
}
void Grow()
{Instantiate(plantPrefab,seedPrefab.position, seedPrefab.rotation)
Destroy(gameObject);
}
I get an error on the .position and .rotation saying “GameObject does not contain a definition for “position” and no accessible extention method “position” accepting a first argument of type “GameObject” could be found (are you missing a using directive or an assembly reference?)”
If I change “public GameObject seedPrefab” to “public Transform seedPrefab” the error goes away, but the plant isn’t spawning where the seed was.
I think I might’ve had an if statement in the update method yesterday, but I forget what it was now. Now I realize it’s just invoking a random plant at a random position in the scene without regard for where the seed is, or if there is one.
I might need an if statement for the Invoke command, which then starts the Grow method if a seed exists? But then I worry how I’m going to differentiate between different seeds if there are multiple planted on the screen at a time.
Maybe I need to figure out how to use a sprite renderer command and not instantiating or using prefabs at all. Sorry I’m so new at this that I dont even know where to start.