Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Could someone help me child this to the object calling this script

Discussion in 'Scripting' started by tawdry, Jun 1, 2018.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi
    I ha a GO named cubity and this script on it to instantiate a object from the resources folder that i would like to be then a child of cubity..
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class sm : MonoBehaviour {
    6.  
    7.     GameObject grass;
    8.  
    9.     void Start(){
    10.      grass = (Resources.Load("sm", typeof(GameObject))) as GameObject;
    11.  
    12.         }
    13.  
    14.     void OnBecameVisible (){
    15.         Instantiate (grass, transform.position, transform.rotation);//as a child of cubity
    16.     }
    17.  
    18.  
    19.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi tawdry,

    If you hover over the word "Instantiate" in your script, you can follow that link. If you then select the top link (Object.Instantiate), it will take you to this help page.

    In your call to Instantiate you have already used "transform.position, transform.rotation". Can you see anything in the aforementioned help page that could allow you to parent the new object to the script's owner?
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    this is what i would do to set Cubity as a child

    Code (CSharp):
    1. void OnBecameVisible (){
    2.         GameObject GO = Instantiate (grass, transform.position, transform.rotation) as GameObject;//as a child of cubity
    3.         GO.transform.parent = this.gameObject.transform;
    4.     }
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Afraid i'm still stuck. The instantiate is a prefab made up of 10 other prefabs if that is important?
    I Tried just throwing the word parent at everything but didnt make any difference.
    Found this line of code
    grass.transform.parent = gameObject.transform; but it returns the below error.

    Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
    UnityEngine.Transform:set_parent(Transform)
    sm:OnBecameVisible() (at Assets/Resources/sm.cs:19)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class sm : MonoBehaviour {
    6.  
    7.     GameObject grass;
    8. //    Vector3 off;
    9.  
    10.  
    11.     void Start(){
    12.      grass = (Resources.Load("sm", typeof(GameObject))) as GameObject;
    13.  
    14.         }
    15.  
    16.     void OnBecameVisible (){
    17.         //Instantiate (grass , transform.position, transform.rotation);
    18.         Instantiate(grass, transform.parent.position,transform.parent.rotation);//this does the same as the coomented out line above it
    19.         grass.transform.parent = gameObject.transform;//this returns that error
    20.         //this.enabled=!this.enabled;//this is able to enable the script even when its been disabled?
    21.     }
    22. }
    23.  
    24.  
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    try this
    Code (CSharp):
    1. void OnBecameVisible (){
    2.         //Instantiate (grass , transform.position, transform.rotation);
    3.         Instantiate(grass, transform.parent.position,transform.parent.rotation,this.gameObject.transform);
    4.        
    5.         //grass.transform.parent = gameObject.transform;//this returns that error
    6.         //this.enabled=!this.enabled;//this is able to enable the script even when its been disabled?
    7.     }
     
  6. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Sweet thx Johne working now:D
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For future reference, you could have used the return value of instantiate to hold a variable to the instance.

    In this case, setting it in the method is great, but I only point this out to show the issue. Since your 'grass' had only referenced the loaded resource, and not the newly created instance (from instantiate). :)
     
  8. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi Methos
    My first language is english but i still hardly ever understand what you are saying. Your explanations are usually way beyond my level of intelligenceo_O.Many Kudos for all the assistance you do give on these forums though.
     
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The issue methos5k is indicating is with this line :
    Code (CSharp):
    1. grass = (Resources.Load("sm", typeof(GameObject))) as GameObject;
    It sets grass to point to the loaded resource. Then later, the following line neither records the new game object, nor parents it to anything.
    Code (CSharp):
    1. Instantiate(grass, transform.parent.position,transform.parent.rotation);
    Maybe something like this is what you want?
    Code (CSharp):
    1. public class sm : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         // This will create the object and parent it to us.
    6.         // It will therefore have our position and rotation.
    7.         grass = Instantiate(
    8.               Resources.Load<GameObject>("sm")
    9.             , transform );
    10.     }
    11.  
    12.     void OnBecameInvisible()
    13.     {
    14.         grass.SetActive(false);
    15.     }
    16.  
    17.     void OnBecameVisible()
    18.     {
    19.         grass.SetActive(true);
    20.     }
    21.  
    22.     GameObject grass;
    23. }
    However, for this to work (i.e. the '(in)visible' methods), the game object containing this script must have a renderer - even if it is not rendering anything.
     
  10. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Ah yes that line was commented out here is the final code.i will try out your code and compare performance ivs the instantiate and destroy route im trying.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class sm : MonoBehaviour {
    6.  
    7.     GameObject grass;
    8.  
    9.     void Start(){
    10.      grass = (Resources.Load("sm", typeof(GameObject))) as GameObject;
    11.  
    12.     }
    13.  
    14.             void OnBecameVisible (){
    15.            
    16.             Instantiate(grass, transform.parent.position,transform.parent.rotation,this.gameObject.transform);
    17.  
    18.         }
    19.                 }
    20.  
    21.  
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry for the confusion :) I try to keep things as easy to understand as I can.
     
  12. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Strangely the set active on off and instantiate/destroy had similar performance.Your code gave me an idea what if i unenable and parent the grass together as they become invisible till i say have 10 objects then destroy instead of destroying 1 at a time that might increase fps?Another option would be to implement a pooling system with your code but my biggest concern is memory so not sure if i want to maintain a massive pool of these objects.
     
  13. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    It is not uncommon to face a trade-off between efficiency of resource use and efficiency of performance. Generally accepted guidance here is to:
    1. Write the code as simply as you can.
    2. Make sure it works and does what you want.
    3. Now check, is it running slowly at all.
    4. If so, use the profiler to find out where and why.
    5. Now, and only now, look to make the 'slow' code faster. Bear in mind that making code more optimised often means making the code harder to follow. That is why this step is generally best left til last. :)