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 Jim_Zee_King · Jan 27, 2015 at 04:19 PM · c#prefab

Making change to an instance of a prefab doesn't work...

Hi all, i have a problem for wich i cannot find any solution online :

I am trying to make change to a child of a prefab that is instantiated by code, but every change i make is not taken into account. For example i tried turning of the mesh renderer, changing scale, position, etc.

Here is the method creating the prefab :

 private GameObject InitPrefab (string a_prefabName, GameObject a_prefabParent = null)
         {
                 GameObject t_prefab = Instantiate (Resources.Load (RA_PREFAB_PATH + a_prefabName, typeof(GameObject))) as GameObject;
                 if (a_prefabParent != null) {
                         t_prefab.transform.parent = a_prefabParent.transform;
                 }
                 
                 return t_prefab;
         }

This does work, and my prefab is created correctly on stage ( the return value is referenced in m_currentPrefab) Here is the method trying to actually make change to some of the prefab children (named : layer0, layer1 and layer2)

 private void SwipeToNextPrefabLayer ()
         {
                 int t_layerIndex = m_currentLayerNum + 1;
                 if (t_layerIndex <= m_currentARScenario.TotalLayers - 1) {
                         GameObject t_layer = m_currentPrefab.transform.GetChildByName ("layer" + t_layerIndex).gameObject;
                         
                         if (t_layer == null) {
                                 Console.Log ("No child found with name : " + "layer" + t_layerIndex);
                                 return;
                         } else
                                 Console.Log ("Currently swiping layer " + t_layer.name);
 
                         //t_layer.GetComponent<MeshRenderer> ().enabled = false;
                         t_layer.transform.localScale = new Vector3 (0f, 0f, 0f);
                         
                         m_currentLayerNum++;
                 } else
                         Console.Log ("Last swipeable layer reached!!!");
         }


GetChildByName is just an extension for Transform, which works too, i do have the child "layer0" returned and can even log its name, but if i try t_layer.transform.localScale = new Vector3 (0f, 0f, 0f); i cannot see anything change!?

Am i forgetting something? It seems my modifications made by code at runtime on an instance are not kept or taken into account...

Any help is welcome, thanks all!

UPDATE : Finally, it was the prefab itself, somehow it got corrupted, and as soon as i recreated it, it worked!

Jim

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
Best Answer

Answer by Owen-Reynolds · Jan 27, 2015 at 04:54 PM

Looks fine to me. Once you Instantiate an object, it's not a prefab anymore. It's just a regular gameObject. There are no special "I came from a prefab" rules. In other words "change to an instance of a prefab" isn't an issue. You're just changing a regular gameObject.

There's probably just a bug somewhere else. Try the usual Debug stuff -- make m_curPrefab public, Pause, and check it points to the right spot; add a color change line (something easy to spot); make a smaller test function, make, sure no other script are interfering.

But, comments: m_currentPrefab is a confusing name. Prefabs are what goes into an Instatiate. The item you get out isn't a prefab.

Vector3.zero is a shortcut for new Vector3(0,0,0);. BUT, instead of setting size to 0, easier to SetActive(false).

I prefer transform.Find("path/name") to getChildByName (more precise.)

Instead of Resourse.Load, often easier to use public GameObject someThing; // prefab dragged here.

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 Jim_Zee_King · Jan 27, 2015 at 05:11 PM 0
Share

Thanks for your answer Owen, i just tested something and it worked, i attached a script to the prefab and saved it, this script contains this method :

 public void RotateChild (string a_childName, float a_x, float a_y, float a_z)
         {
                 gameObject.transform.GetChildByName (a_childName).localEulerAngles = new Vector3 (a_x, a_y, a_z);
         }

And when i reference the script from outside i can make change to the prefab's instance child like so : t_script.RotateChild ("layer0", 45f, 45f, 45f);, this works, could this point to the root of the issue? You are right about my member name, ill refactor it :)

As for the other comments, for the current project i am on, i must load the prefab from the resources and i must say that everything else works great, prefab is shown and loaded correctly and there isn't a single warning in the debug...

Thansk four your hints!!

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

19 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

Animator is not playing a Playable 2 Answers

Save and load prefabs instantiated at runtime 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