Quantcast
Channel: Questions in topic: "clone"
Viewing all articles
Browse latest Browse all 508

Change instantiated prefab's rigidbody Velocity

$
0
0
I have several prefabs from an object pool being instantiated by a coroutine. I want to change the velocity of the instances to increase after certain points in time. I'm attempting to do this using the invoke method, but it seems it's not affecting the prefab clones instantiated by my spawner script. My spawner script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class collectableSpawner : MonoBehaviour { public GameObject[] collectablePrefabs; public float delay = 1.0f; public bool active = true; public Vector2 delayRange = new Vector2(1,2); // Use this for initialization void Start () { resetDelay(); StartCoroutine(EnemyGenerator ()); } IEnumerator EnemyGenerator(){ yield return new WaitForSeconds(delay); if(active){ var newTransform = transform; gameObjectUtility.Instantiate(collectablePrefabs[Random.Range(0, collectablePrefabs.Length)], newTransform.position); // remove var newItem to reset resetDelay(); } StartCoroutine(EnemyGenerator ()); } void resetDelay(){ delay = Random.Range(delayRange.x, delayRange.y); } } Velocity Script The velocity never gets updated by the invoke function on the instantiated prefab clones. using System.Collections; using System.Collections.Generic; using UnityEngine; //[SelectionBase] public class instantVelocity : MonoBehaviour { public Vector2 velocity = Vector2.zero; public Vector2 speedUp = new Vector2(1.20f, 0f); private Rigidbody2D body2d; void Awake(){ body2d = GetComponent(); if(body2d.tag == "Collectable"){ body2d.velocity = velocity; } } void FixedUpdate(){ body2d.velocity = velocity; InvokeRepeating("goFaster", 20.0f, 20.0f); } public void goFaster(){ body2d.velocity = Vector2.Scale(velocity, speedUp); } } And the gameObjectUtility script referenced in my spawner public class gameObjectUtility { private static Dictionary pools = new Dictionary(); public static GameObject Instantiate(GameObject prefab, Vector3 pos){ GameObject instance = null; var recycleScript = prefab.GetComponent(); if(recycleScript != null){ var pool = GetObjectPool(recycleScript); instance = pool.NextObject(pos).gameObject; } else { instance = GameObject.Instantiate(prefab); instance.transform.position = pos; } return instance; } public static void Destroy(GameObject gameObject){ var RecycleGameObject = gameObject.GetComponent(); if (RecycleGameObject != null){ RecycleGameObject.ShutDown(); } else { GameObject.Destroy(gameObject); } //GameObject.Destroy(gameObject); } private static objectPool GetObjectPool(recycleGameObject reference){ objectPool pool = null; if (pools.ContainsKey(reference)){ pool = pools[reference]; } else { var poolContainer = new GameObject(reference.gameObject.name + "objectPool"); pool = poolContainer.AddComponent(); pool.prefab = reference; pools.Add(reference, pool); } return pool; } } I'm very new to Unity & c#. I feel that there's something fundamental about C# scripting or unity or both that i'm completely overlooking or perhaps some rule or best practice i'm ignorant of. I've wasted about 7 cumulative hours trying to figure it out to no avail. Any help is greatly appreciated.

Viewing all articles
Browse latest Browse all 508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>