- Home /
Clone of prefabs copy original´s script state during runtime?
I mean... If i have a prefab with a component-script and I instantiate it during runtime more then once, the clone instances comes with its variables updated in relation to the preview (copied) instance? The thing is. I have this script in my prefabs but their are returning "hitcount" with wrong values. Sometimes it returns 3, sometimes, 2, sometimes 1. And seems to be random! What also annoys me is that before I used "cdScript.hitcount = cdScript.hitcount + 1;" but also didnt work. Then I tried this explicit value and steel something goes wrong.
void Start () { cd = GameObject.FindGameObjectWithTag("Player"); cdScript = cd.GetComponent(); }
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
hitInfo = new RaycastHit ();
hit = Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo);
if (hit)
{
if (hitInfo.transform.gameObject.tag != "Stuff")
{
return;
}
else
{
if (!picked)
{
StartCoroutine (TeleTime ());
}
else
{
return;
}}}}}
IEnumerator TeleTime ()
{
switch (cdScript.hitcount)
{
case 0:
hitInfo1 = hitInfo;
cdScript.hitcount = 1;
hitInfo1.transform.gameObject.GetComponent<Picked>().picked = !picked;
hitInfo1.transform.gameObject.transform.position = new Vector3 (hitInfo.transform.gameObject.transform.position.x, 1f, hitInfo.transform.gameObject.transform.position.z);
hitInfo1.transform.gameObject.GetComponent<Rigidbody> ().isKinematic = true;
yield return new WaitForSeconds (teletime);
cdScript.hitcount = cdScript.hitcount - 1;
hitInfo1.transform.gameObject.GetComponent<Rigidbody> ().isKinematic = false;
hitInfo1.transform.gameObject.GetComponent<Picked>().picked = !picked;
break;
}
} etc...
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How to store a prefab in a variable 1 Answer
Issue Instantiating prefab in C# 0 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer