Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by uanmanarmy · Aug 11, 2014 at 11:56 AM · gameobjecttransformchilditems

Trying to find objects.

Hello.

Im trying to find objects after they are instantiated.

So here are the function that Create Items, DeleteThem

 public void CreateItems()
     {
         numberOfObjects = objectScript.ItemsNumber;
         string letter = objectScript.ItemsColor;
             //impar
             if(numberOfObjects % 2 != 0)
             {
                 iterator = -numberOfObjects +1;
                 offsetX = 2f;
                 int j = 0;    
                     for(i = iterator; i < numberOfObjects  ; i += offsetX)
                     {
                         ReadAString(letter,j,i , yPosition);
                         j++;
                     }
 
             }
             
             //par
             else 
             {
                 iterator = - numberOfObjects +1;
                 offsetX = 2.0f;
                 if(numberOfObjects !=0 )
                 {    
                     int j = 0;
                     for(i = iterator; i < numberOfObjects ; i += offsetX)
                     {
                         ReadAString(letter, j, i, yPosition);
                         j++;
                     }
                 }
             }
 
             // Check for exceptions case
             if(numberOfObjects == 0)
             {
                 return;
             }
 
     }
 
     public void DeleteItems()
     {
         // Destroy any Items when The function is Calleds
         foreach (GameObject i in prefabs)
             Destroy (i);
         
         // Clear List
         prefabs.Clear ();
     }
 
     public void ReadAString(string letter,int j, float i, float yPosition)
     {    
             // Get the specific Letter
             switch(letter[j])
             {
             case 'B':
                 cube = Item_bbg;
                 break;
                 
             case 'G':
                 cube = Item_gbg;
                 break;
                 
             case 'O':
                 cube = Item_obg;
                 break;
                 
             case 'P':
                 cube = Item_pbg;
                 break;
             case 'R':
                 cube = Item_rbg;
                 break;
             case 'Y':
                 cube = Item_ybg;
                 break;
                 
             default: break;
             }
 
             //Create a Clone of Item with the specific Prefab and Color
             GameObject clone = Instantiate (cube, new Vector3 (i , yPosition, -1), Quaternion.identity) as GameObject;
             //Add the Clone in a list for a further use
             prefabs.Add(clone);
 
     }


Now I want to find an Item (Clone) that was instantiated.And Find it's child

This are variables for Items

 [HideInInspector]
     public Vector3 blueScale;
     [HideInInspector]
     public Vector3 orangeScale;
     [HideInInspector]
     public Vector3 yellowScale;
     [HideInInspector]
     public Vector3 purpleScale;
     [HideInInspector]
     public Vector3 greenScale;
     [HideInInspector]
     public Vector3 redScale;
     
     
     public float blueEnergy;
     public float greeEnergy;
     public float orangeEnergy;
     public float purpleEnergy;
     public float redEnergy;
     public float yellowEnergy;
     
     
     [HideInInspector]
     public Transform blue;
     [HideInInspector]
     public Transform green;
     [HideInInspector]
     public Transform orange;
     [HideInInspector]
     public Transform purple;
     [HideInInspector]
     public Transform red;
     [HideInInspector]
     public Transform yellow;
     
     public GameObject _blue;
     public GameObject _green;
     public GameObject _orange;
     public GameObject _purple;
     public GameObject _red;
     public GameObject _yellow;


I made an Function

 public void FindItemsObjects(GameObject _Blue, Transform Blue, Vector3 BlueScale, GameObject _Green, Transform Green, Vector3 GreenScale,
                                  GameObject _Orange, Transform Orange, Vector3 OrangeScale, GameObject _Purple, Transform Purple, Vector3 PurpleScale,
                                  GameObject _Red, Transform Red, Vector3 RedScale, GameObject _Yellow, Transform Yellow, Vector3 YellowScale)
     {
 
 
         //energyBarSprite = bluePrefab.GetComponent<SpriteRenderer> ();
         _Blue = GameObject.Find ("Item_bbg(Clone)");
         Blue = FindChild (_Blue, "line");
         Debug.Log (Blue);
         _Green = GameObject.Find ("Item_gbg(Clone)");
         Green = FindChild (_Green, "line");
 
         _Orange = GameObject.Find ("Item_obg(Clone)");
         Orange = FindChild (_Orange, "line");
 
         _Purple = GameObject.Find ("Item_pbg(Clone)");
         Purple = FindChild (_Purple, "line");
 
         _Red = GameObject.Find ("Item_rbg(Clone)");
         Red = FindChild (_Red, "line");
 
         _Yellow = GameObject.Find ("Item_ybg(Clone)");
         Yellow = FindChild (_Yellow, "line");
 
         BlueScale = Blue.localScale;
         OrangeScale = Orange.localScale;
         YellowScale = Yellow.localScale;
         PurpleScale = Purple.localScale;
         GreenScale = Green.localScale;
         RedScale = Red.localScale;
     }

And Now I call it.

 if(GUI.Button(new Rect(10,120,100,50), "New Level"))
         {    
 
 ;
 
             autoObject.DeleteItems();
             autoObject.CreateItems();
             FindItemsObjects(_blue, blue, blueScale, _green, green, greenScale, _orange, orange, orangeScale,
                              _purple, purple, purpleScale, _red, red, redScale, _yellow, yellow, yellowScale);
 
             
         }


Ok if Im putting an Debug.Log in the FindItemsObject

like Debug.Log (Blue); it will find me the coresponding object in the game. That's good.

Now if I want to use this variable from another script it says to me that its null

 UpdateEnergyBar (menuScript.blue, blueEnergy, blueScale)

and this is the Update Energy bar Function

  public void UpdateEnergyBar (Transform energySprite, float currentEnergy, Vector3 energyScale)
         {    
             Debug.Log ("EnergySprite " + energySprite.name);
             energySprite.transform.localScale = new Vector2 (energyScale.x + currentEnergy , 1);
             Debug.Log ("energySprite scale on x axes   " + energySprite.transform.localScale.x);
 
         }



What Im doing here is Create Items and Find their Child (for the first time it working) Then When I pres New Level it must delete the old Items, Create new Items and Find Again their children, but here is the problem they are not found, because the compiler says me that the transforms was deleted , that means that was deleted when I Delete the Items.

But what is not normal for me is that After I delete Im creating again the Items and then Im searching again for his child but I dont know why it's not showing, Please help me :d

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mediumal · Aug 11, 2014 at 01:37 PM

You are not setting the blue variable properly so it remains the old (now deleted) value.

This is because FindItemsObjects() is passed the blue variable by value, instead of by reference. I suggest doing some research on the C# ref keyword.

However, you don't need to pass any parameters at all to FindItemsObjects() since I assume it is part of the Items class. Just change Blue to blue, _Blue to _blue, etc.

Comment
Add comment · Show 1 · 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 uanmanarmy · Aug 12, 2014 at 08:29 AM 0
Share

how can i do it, can you give an example I dont get it . Please

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

22 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

Related Questions

Finding Children question 3 Answers

Get Child from a parent 3 Answers

Properly Rotating Child Objects by Script 1 Answer

Error code CS0029. 1 Answer

Can't Return the Tag of a Child Object 4 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