• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FuryFight3r · May 28, 2017 at 07:20 AM · gameobjectinstantiateprefabprefab-instance

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;
         }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AmbushSky · May 28, 2017 at 07:23 AM 0
Share

Try something like:

 GameObject plant = Instantiate(cottonPlant, lastPos, Quaternion.identity);
 plant.transform.SetParent(dirt.transform);

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Masterio · May 28, 2017 at 07:30 AM 0
Share

use SetParent() ins$$anonymous$$d parent.

https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

avatar image FuryFight3r Masterio · May 28, 2017 at 07:39 AM 0
Share

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);
         }
 
     }
 }
 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn Prefabs 1 Answer

How to instantiate a prefab with a script attached? 2 Answers

I cant delete multiple instantiated prefabs 0 Answers

Instantiating to gameobject.transform.position 1 Answer

Retrieving GameObjects from a Prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges