- Home /
Modifying/Calling Instantiated Objects
Hey there guys, I'm a little bit stuck on modifying instantiated objects, it seems a bit too difficult for me to work out, what i am trying to achieve is the object that instantiates, I want it to be moved to a child of an object, but i can see what I'm doing wrong, I am trying to move the prefab, not the newly instantiated object, how can i control the instantiated object rather than the prefab to instantiate? this is the c# code i currently have.
if (other.CompareTag("seed"))
{
Destroy(other);
lastPos = other.transform.position;
Instantiate(cottonPlant, lastPos, Quaternion.identity);
//HERE IS THE ISSUE
cottonPlant.transform.parent = dirt.transform;
}
if (other.CompareTag("dfseed"))
{
Destroy(other);
lastPos = other.transform.position;
Instantiate(dragonPlant, lastPos, Quaternion.identity);
//HERE IS THE ISSUE
dragonPlant.transform.parent = dirt.transform;
}
Try something like:
GameObject plant = Instantiate(cottonPlant, lastPos, Quaternion.identity);
plant.transform.SetParent(dirt.transform);
Answer by FuryFight3r · May 28, 2017 at 07:25 AM
I have found a solution,
if (other.CompareTag("seed"))
{
Destroy(other);
lastPos = other.transform.position;
GameObject gO = Instantiate(cottonPlant, lastPos, Quaternion.identity) as GameObject;
gO.transform.parent = dirt.transform;
}
if (other.CompareTag("dfseed"))
{
Destroy(other);
lastPos = other.transform.position;
GameObject gO = Instantiate(dragonPlant, lastPos, Quaternion.identity) as GameObject;
gO.transform.parent = dirt.transform;
}
simply by putting GameObejct 'name' then instantiate i have made a recognizable object that can be used to modify the instantiated prefab.
use SetParent() ins$$anonymous$$d parent.
https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
Thanks for the input, but I'm unsure of how this will interline with my other code calling from that dirt GameObject, as it has to be a Transform, not a GameObject and when i change it to Transform, it messes with everything else that is already working off dir as a GameObject.. im sorry im not the greatest at explaining things haha, this is the code i have if you think there may be a way around this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plantingScript : $$anonymous$$onoBehaviour {
float fertTimer = 600;
float maxFertTimer = 600;
bool fertAdded = false;
public GameObject cottonPlant;
public GameObject dragonPlant;
ParticleSystem fertParts;
public GameObject fertPartsGo;
public GameObject eFert;
GameObject dirt;
Vector3 lastPos;
Quaternion lastRot;
// Use this for initialization
void Start () {
dirt = gameObject;
fertParts = dirt.transform.GetChild(0).GetComponent<ParticleSystem>();
fertParts.Stop();
gameObject.GetComponent<BoxCollider>().enabled = true;
}
// Update is called once per frame
void Update () {
if(fertTimer <= 0)
{
fertAdded = false;
fertTimer = maxFertTimer;
}
if (fertAdded)
{
fertTimer -= Time.deltaTime;
}
if (!fertAdded)
{
fertTimer = maxFertTimer;
fertParts.Stop();
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("seed"))
{
Destroy(other);
lastPos = other.transform.position;
GameObject go = Instantiate(cottonPlant, lastPos, Quaternion.identity) as GameObject;
cottonPlant.transform.parent = dirt.transform;
}
if (other.CompareTag("dfseed"))
{
Destroy(other);
lastPos = other.transform.position;
GameObject go = Instantiate(dragonPlant, lastPos, Quaternion.identity) as GameObject;
go.transform.parent = dirt.transform;
}
if (!fertAdded && other.CompareTag("bagoffert"))
{
fertAdded = true;
lastPos = other.transform.position;
lastRot = other.transform.rotation;
other.GetComponent<BoxCollider>().enabled = false;
Destroy(other);
fertParts.Play();
Instantiate(eFert, lastPos, lastRot);
}
}
}