Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by deraggi · Oct 08, 2016 at 01:04 PM · gameobjectfunction

Temporary Gameobject Reference in Function without Instantiating

Hey everyone,

this might seem obvious for everyone intermediately experienced with C#, but I'm struggeling a bit with Gameobect variables in functionsproject.

I have a class with multiple GO Attributes, like:

 GameObject trees;
 GameObect cars;
 GameObject houses;

Now, I would like to get the child of ony of those objects in one funtion. This is just an example:

 public GameObect getChild(myTypeEnum type) 
 
  GameObject parent = new GameObject();
         switch (type)
         {
             case (myTypeEnum .house):
                 if (houses == null)
                    parent = houses;
                 break;
             case (myTypeEnum.cars):
                 if (cars == null)
                     parent = cars;
                 break;
             case (myTypeEnum.trees):
                 if (trees== null)
                     parent = trees;
                 break;
             default:
                 break;
         }
 
 return parent.transform.GetChild(0)

My problem is now that due to the new Gameobject() statement, a GO is instantiated in the hirarchy which I cannot destroy as I need to return it. I am sure there is a better way to do this...

Anyhelp is much appreciated.

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 deraggi · Oct 11, 2016 at 06:41 PM 0
Share

Cmon guys, does nobody know this?

2 Replies

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

Answer by TBruce · Oct 11, 2016 at 07:55 PM

Going by your code you have something like this (note: everything here was placed in one class)

 using UnityEngine;
 using System.Collections;
 
 public enum ArrayType {house, cars, trees}
 
 public class MyClass : MonoBehaviour
 {
     GameObject trees;
     GameObect cars;
     GameObject houses;
 
     public GameObect getChild(myTypeEnum type)
     {
         GameObject parent = new GameObject();
         switch (type)
         {
             case (myTypeEnum .house):
                 if (houses == null)
                     parent = houses;
                 break;
             case (myTypeEnum.cars):
                 if (cars == null)
                     parent = cars;
                 break;
             case (myTypeEnum.trees):
                 if (trees== null)
                     parent = trees;
                 break;
             default:
             break;
         }
         return parent.transform.GetChild(0)
     }
 }

Now to return the first child of one of those objects in the getChild() funtion you can do it like this

 using UnityEngine;
 using System.Collections;
 
 public enum ArrayType {house, cars, trees}
 
 public class MyClass : MonoBehaviour
 {
     GameObject trees;
     GameObect cars;
     GameObject houses;
 
     public GameObect getChild(myTypeEnum type)
     {
         // if you only want the child you can return null or new GameObject();
         // default to null
         GameObject go = null;
         switch (type)
         {
             case (myTypeEnum .house):
                 go = houses;
                 break;
             case (myTypeEnum.cars):
                 go = cars;
                 break;
             case (myTypeEnum.trees):
                 go = trees;
                 break;
             default:
             break;
         }
 
         // if go is not null and go has at least one child return it
         // else return go
         if ((go != null) && (go.transform.childCount > 0))
         {
             return houses.transform.GetChild(0).gameObect;
         }
         else return go;
     }
 }
Comment
Add comment · 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
0

Answer by deraggi · Oct 11, 2016 at 08:55 PM

Actually, this

 GameObject go = null;

was what I needed. Did not think of that myself and were not able to find it.

Thanks you so much!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

90 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

Related Questions

random an image after a event ends 0 Answers

How to Access Values from a Object,How to access values from a object 0 Answers

I need to activate a function in another script from a script on a different game object 0 Answers

collider.gameObject.GetComponent doesn't have correct values yet it has the correct name 0 Answers

Texture turn black and Gameobject become half or unseen-able 0 Answers


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