- Home /
A growing problem. the children keep getting bigger.
I'm working on a Unity game where the player must pick up objects that then follow him. These objects are parented to the player before it makes a new instantiation of the object. However, this causes the new object that is spawned to be larger in size. Here is my code:
using UnityEngine;
using System.Collections;
public class Pickery : MonoBehaviour
{
public int Counter = 100;
public GameObject parson;
public int Timekeeper = 10;
public int Collection = 0;
public GameObject cameran;
void OnTriggerEnter (Collider other)
{
Debug.Log ("Counter:" + Counter);
cameran.SendMessage("Plus", Timekeeper);
cameran.SendMessage("Wumbo",Counter);
parson.transform.parent = transform;
Collection++;
parson.transform.localPosition = new Vector3(0, 0, -Collection);
parson = Instantiate(parson, new Vector3(Random.Range(10,-35),921,Random.Range(267,310)), Quaternion.identity)as GameObject;
}
}
I believe the cause of this problem is when it is picked up by the parent. removing the two lines that parent the object to the player causes the objects not to grow, but doesn't put them behind the player(puts them at 0,0, - something). Is there a way I can have it ignore the parent's size when it instantiates a new object, or have it place the objects behind the player and following the player with the parenting lines removed?
Answer by coastwise · Nov 29, 2012 at 04:47 PM
The trouble is that when you apply the parent, the child will change its scale to stay the same size. For example, the player is scaled by 0.9, the other object is 1. I make the player the parent, the child changes its scale to 1.11 so that it looks the same. (0.9 * 0.111 = 1)
I can see to straightforward ways to solve this: instantiate the new object before parenting the old one to the player, or have a reference to a prefab that you instantiate instead of copying one that already exists in game (this will also avoid (Clone) (Clone) (Clone)).
Here is my take on the first proposed solution to the code you posted above:
using UnityEngine;
using System.Collections;
public class Pickery : MonoBehaviour {
public int Counter = 100;
public GameObject parson;
public int Timekeeper = 10;
public int Collection = 0;
public GameObject cameran;
void OnTriggerEnter (Collider other) {
Debug.Log ("Counter:" + Counter);
cameran.SendMessage("Plus", Timekeeper);
cameran.SendMessage("Wumbo",Counter);
Collection++;
parson.transform.localPosition = new Vector3(0, 0, -Collection);
GameObject newparson = Instantiate(parson, new Vector3(Random.Range(10,-35),921,Random.Range(267,310)), Quaternion.identity)as GameObject;
parson.transform.parent = transform;
parson = newparson;
}
}
Got that to work after putting the line where it re-locates the object after where it parents the object.
Like so:
GameObject newparson = Instantiate(parson, new Vector3(Random.Range(10,-35),921,Random.Range(267,310)), Quaternion.identity)as GameObject; parson.transform.parent = transform; parson.transform.localPosition = new Vector3(0, 0, -Collection); parson = newparson;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GetComponentInChildren with tag in two variable 3 Answers
Moving a GameObject reffering to the coordinates of the parent Object 1 Answer
How to calculate required rotation of a parent object by a given rotation of a child 1 Answer