- Home /
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
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
.
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
Follow this Question
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