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 /
avatar image
0
Question by Obsdark · Sep 14, 2017 at 03:05 AM · c#scripting problemprefabinstanceprefab-instance

How to obtain this prefab name in this circunstances

The prefab in question is instanciated on an object who is created at runtime, inside a variable in a component (script) called PrefabAdapt which is also set at runtime, it is also a children from the GameObject with the component attached and thats also setted at runtime.

This prefab is called Trolords(Clone) and the variable is a GameObject variable type.

This is a picture who show you the hierarchys and components

alt text

How can i obtain the prefab from the tile in order to take him's name to add it to a save file?

I already try:

 GetComponent().thisPrefab.name //Doesn't work
 GetComponent().thisPrefab.gameObject.name //Doesn't work
 GetComponent().thisPrefab.gameObject.transform.name //Doesn't work
 GetComponent().thisPrefab.transform.name //Doesn't work
 GetComponent().thisPrefab.ToString() //Doesn't work
 GetComponent().thisPrefab //Doesn't work
 
     foreach (Transform child in world1.TilesGO[0,0].transform)
     {
              Debug.Log(child.name); // YES USE .name AS IS DON"T USE thisPrefabName
     }

Additional info

Aparently, for some reason, the object return null when i request it from the WorldController start() function to use it, but the thing is, as you can see in the image, the object still seems to exist in that variable which, by some reason, is returning null in the WorldController start() function.

If you want my guess, thanks to the coment of one user, i'm guessing than the problem is than, the PrefabAdapt is somewhat not ready to load the components at the moment the WorldControler start() function is working, so a good solution would be to make wait the parent function of the addition of the PrefabAdapt method if possible.

Did you know how to make a function wait for another function before start to run?

here is the code of the component, any help would be much apreciated, thanks in advance:

 using Assets;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PrefabAdapt : MonoBehaviour {
 
     public bool needToDo = true;
     public Tile tile;
     public GameObject tileGO;
     public GameObject thisPrefab;
     public string thisPrefabName;
     public string nameWorld;
 
     //To Resize the Grid
     private float AdditionalModifierGridResizer = 0f;
     private float scaleModFactor;
 
     void Start()
     {
         AdaptTo3D();
     }
 
     private bool AdaptTo3D()
     {
         try
         {
             if (needToDo)
             {
                 if (tileGO != null) // Work with Tile and Prefab acording to case
                 {
                     //Instance the prefab
                     thisPrefab = Instantiate(WorldController.Instance.prefabForTiles, tileGO.transform.position, Quaternion.identity);
                 }
                 else
                 {
                     thisPrefab = Instantiate(WorldController.Instance.prefabForTiles, tile.tile_GO.transform.position, Quaternion.identity);
                 }
 
                 //<--------------------------------------------->
                 //Make sure than can interact with MouseController
                 if (!thisPrefab.GetComponent<BoxCollider>())
                     thisPrefab.AddComponent<BoxCollider>();
 
                 thisPrefab.GetComponent<BoxCollider>().transform.SetGlobalScale(thisPrefab.transform.lossyScale);
                 //<--------------------------------------------->
 
                 //Fix automaticaly the size of the grid to fit the Grid to the new Tiles & Prefab Size
                 AdditionalModifierGridResizer = WorldController.Instance.GridResizer;
                 scaleModFactor = Mathf.Floor(thisPrefab.GetComponent<MeshRenderer>().bounds.size.x) + AdditionalModifierGridResizer;
                 
                 if (tileGO != null) // Work with Tile and Prefab acording to case
                 {
                     if (WorldController.Instance.worlds[tileGO.transform.parent.gameObject.name].Wrld_GO.transform.localScale.x < scaleModFactor)
                         WorldController.Instance.worlds[tileGO.transform.parent.gameObject.name].Wrld_GO.transform.localScale = new Vector3(scaleModFactor, scaleModFactor, 10f);
 
                     nameWorld = tileGO.transform.parent.gameObject.name;
                 }
                 else
                 {
                     if (WorldController.Instance.worlds[tile.World].Wrld_GO.transform.localScale.x < scaleModFactor)
                         WorldController.Instance.worlds[tile.World].Wrld_GO.transform.localScale = new Vector3(scaleModFactor, scaleModFactor, 10f);
 
                     nameWorld = tile.World;
                 }
 
                 if (tileGO != null) // Work with Tile and Prefab acording to case
                 {
                     thisPrefab.transform.parent = tileGO.transform;
                     thisPrefabName = thisPrefab.name;
                     tileGO.transform.parent = WorldController.Instance.worlds[nameWorld].Wrld_GO.transform;
 
                     if (WorldController.Instance.worlds[tileGO.transform.parent.gameObject.name].Wrld_GO.transform.eulerAngles.x != -90f)
                     {
                         Vector3 newVector = new Vector3(-90f, 0f, 0f);
                         WorldController.Instance.worlds[tileGO.transform.parent.gameObject.name].Wrld_GO.transform.localRotation = Quaternion.Euler(newVector.x, newVector.y, newVector.z);
 
                         //Fix tile[0,0 rotation
                         if(WorldController.Instance.worlds[nameWorld].TilesGO[0, 0].GetComponent<PrefabAdapt>())
                         {
                             if (WorldController.Instance.worlds[nameWorld].TilesGO[0, 0].GetComponent<PrefabAdapt>().thisPrefab.transform.eulerAngles.x != 90f)
                             {
                                 thisPrefab.transform.localRotation = Quaternion.Euler(newVector.x, newVector.y, newVector.z);
                             }
                         }
                         
                     }
 
                     if (WorldController.Instance.worlds[nameWorld].TilesGO[0, 0].GetComponent<PrefabAdapt>() &&
                    WorldController.Instance.worlds[nameWorld].TilesGO[0, 1].GetComponent<PrefabAdapt>())
                     {
                         if (WorldController.Instance.worlds[nameWorld].TilesGO[0, 0].transform.localScale != WorldController.Instance.worlds[nameWorld].TilesGO[0, 1].transform.lossyScale)
                         {
                             //Setting right the Tile universal Position
                             //ExtensionMethods.SetGlobalScale(WorldController.Instance.worlds[tile.World].Tiles[0, 0].tile_GO.transform, WorldController.Instance.worlds[tile.World].Tiles[0, 1].tile_GO.transform.lossyScale);
 
                             //Setting right the Prefab of the Tile universal Position (Problems)
                             ExtensionMethods.SetGlobalScale(WorldController.Instance.worlds[nameWorld].TilesGO[0, 0].GetComponent<PrefabAdapt>().thisPrefab.transform, thisPrefab.transform.lossyScale);
                         }
 
                     }
                 }
                 else
                 {
                     thisPrefab.transform.parent = tile.tile_GO.transform;
                     tile.tile_GO.transform.parent = WorldController.Instance.worlds[tile.World].Wrld_GO.transform;
 
                     if (WorldController.Instance.worlds[tile.World].Wrld_GO.transform.eulerAngles.x != -90f)
                     {
                         Vector3 newVector = new Vector3(-90f, 0f, 0f);
                         WorldController.Instance.worlds[tile.World].Wrld_GO.transform.localRotation = Quaternion.Euler(newVector.x, newVector.y, newVector.z);
 
                         //Fix tile[0,0 rotation
                         if (WorldController.Instance.worlds[tile.World].Tiles[0, 0].tile_GO.GetComponent<PrefabAdapt>().thisPrefab.transform.eulerAngles.x != 90f)
                         {
                             thisPrefab.transform.localRotation = Quaternion.Euler(newVector.x, newVector.y, newVector.z);
                         }
                     }
 
                     if (WorldController.Instance.worlds[tile.World].Tiles[0, 0].tile_GO.GetComponent<PrefabAdapt>() &&
                    WorldController.Instance.worlds[tile.World].Tiles[0, 1].tile_GO.GetComponent<PrefabAdapt>())
                     {
                         if (WorldController.Instance.worlds[tile.World].Tiles[0, 0].tile_GO.transform.localScale != WorldController.Instance.worlds[tile.World].Tiles[0, 1].tile_GO.transform.lossyScale)
                         {
                             //Setting right the Tile universal Position
                             //ExtensionMethods.SetGlobalScale(WorldController.Instance.worlds[tile.World].Tiles[0, 0].tile_GO.transform, WorldController.Instance.worlds[tile.World].Tiles[0, 1].tile_GO.transform.lossyScale);
 
                             //Setting right the Prefab of the Tile universal Position (Problems)
                             ExtensionMethods.SetGlobalScale(WorldController.Instance.worlds[tile.World].Tiles[0, 0].tile_GO.GetComponent<PrefabAdapt>().thisPrefab.transform, thisPrefab.transform.lossyScale);
                         }
 
                     }
                 }              
             }
 
             return false;
         }
         catch(Exception ex)
         {
             Debug.Log("PrefabAdapt Error: "+ex.ToString());
             return false;
         }
     }
 
 }
 

This is the place where i'm calling the methods:

WorldController Class: (the one who calls the construction of the world, who owns the tiles who have the PrefabAdapt script like component)

  void OnEnable()
     {
         if (Instance != null)
         {
             Debug.Log("There should never be two world controllers, just can be one!");
         }
         Instance = this;
     }
 
     void Start()
     {
         //Create a world with empty tiles
         World world1 = World.CreateWorld(32, "some3DWorld", true, true); //The bool controls between 2D and 3D
         World world2 = World.CreateWorld(32, "some2DWorld", false, false);
         world2.Wrld_GO.transform.localPosition = new Vector3(0f, -36f, -42f);
         world2.Wrld_GO.transform.localRotation = Quaternion.Euler(new Vector3(-90f, 0f, 0f));
         world2.Wrld_GO.transform.SetGlobalScale(new Vector3(17f, 17f, 1f));
 
         Camera.main.transform.position = new Vector3(17f, 552f, -107f);
         //Camera.main.transform.rotation = Quaternion.Euler(new Vector3(35.68f,0f,0f));
         Camera.main.transform.rotation = Quaternion.Euler(new Vector3(90f, 0f, 0f));
         Camera.main.transform.SetGlobalScale(world1.Wrld_GO.transform.lossyScale);

         /*Debug.Log(world2.Tiles[0, 0].PrefabSprite);
         Debug.Log(world1.TilesGO[0, 0].GetComponent<PrefabAdapt>().tile.PrefabSprite);
         Debug.Log(world1.TilesGO[0, 0].GetComponent<Instance>().Tile.PrefabSprite);*/
     }
 
     void Update ()
     {
         //Debug.Log(this.worlds["some3DWorld"].Tiles[0, 0].PrefabSprite);
         
         Debug.Log(this.worlds["some3DWorld"].TilesGO[0, 0].GetComponent<Instance>().Tile.PrefabSprite);
     }

If i call it in the Update() method, it works perfectly, the problem is i wanna call it in th Start() method of the WorldController object, is there any way to do that?

Because otherwise the object shows null, and the Tile show itself with no childs (in code lvl at least).

So i think the solution would be make the construction of the world wait for the PrefabAdapt, but i don't know exactly how to do that.

Is there any way to do than a function would be waited before continue the work of the original function?

Thanks in advance

hierarchy.png (168.6 kB)
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
0

Answer by Cornelis-de-Jager · Sep 14, 2017 at 03:26 AM

There are two ways to go about this, and read both and the last segment, please. The first is the way you tried:

 // If its run from a script attached to the tile
 GetComponent<PrefabAdapt> ().thisPrefabname;
 
 // Or if its run from somewhere else
 Tile_0_0.GetComponent<PrefabAdapt> ().thisPrefabname;

if you run it from a script that is attached to the Tile_0_0 game object it should work. If it doesn't you can also try the following method:

 foreach (Transform child in transform) {
   nameIWant = child.name; // YES USE .name AS IS DON"T USE thisPrefabName
 }

Finally looking at your code you convert it to a 3D object in the Start function. This might cause a problem because the Start function can be run before the object is assigned to the parent. (You did say that it was instantiated). To test this create a button that calls the convert function when clicked. If it works then that is the issue.

Comment
Add comment · Show 3 · 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 Obsdark · Sep 14, 2017 at 01:15 PM 0
Share

I'm gonna test it out when i finish work, however if that was the case, a problem of not yet assigned as you suggest with the button stuff, what would be the solution?, did you see any solution internaly for this code particle?, Thanks for the suggestion.

avatar image Obsdark · Sep 16, 2017 at 01:03 AM 0
Share

It doesn't work, any other ideas :S

avatar image Obsdark · Sep 17, 2017 at 08:21 PM 0
Share

Indeed Cornelis-de-Jager, that was the issue, any idea how to fix it?

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

409 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 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 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 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 avatar image

Related Questions

How to Instantiate prefab when dragging an object through a specific area? 1 Answer

Multiple Cars not working 1 Answer

Changing a Prefab's Script's Variables 1 Answer

How can I access specific children of a prefabs I initiate in a code 2 Answers

Unity 2D : How to make a perfect clone of my Game Object? 3 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