TheUninvited
Active Coder
It gives me a wrong position but i dont know why? what am i doing wrong and should i do instead?
My cubesScript:
My SpawnerCubes script:
This is what is happening:
My cubesScript:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeBase : MonoBehaviour
{
public int maxHits;
public int timesHit;
public Texture[] textures;
public float changeInterval = 0.33F;
public Renderer rend;
void Awake()
{
}
void Start()
{
SpawnerCubes.Instance.CubesAdder.Add(this.gameObject);
rend = GetComponent<Renderer>();
}
void Update()
{
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "ball")
{
timesHit++;
if (timesHit >= maxHits)
{
Destroy(gameObject);
SpawnerCubes.Instance.CubesAdder.Remove(this.gameObject);
}
else
{
ChangeTextureWhenHit();
}
}
}
private void ChangeTextureWhenHit()
{
int textureIndex = timesHit;
if (textureIndex >= textures.Length)
{
textureIndex = textures.Length - 1;
}
//Debug.Log("texture index is" + textureIndex);
rend.material.mainTexture = textures[textureIndex];
}
}
My SpawnerCubes script:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnerCubes : MonoBehaviour
{
public List<GameObject> cubeSpawner = new List<GameObject>();
private Queue<GameObject> availableObjects = new Queue<GameObject>();
public GameObject pubInstance;
public List<GameObject> CubesAdder = new List<GameObject>();
public List<Vector3> transfPos = new List<Vector3>();
public Vector3 startingPos;
public int counter;
public float timer = 2f;
public float duration = 2f;
Vector3 getVector;
public Text randText;
public static SpawnerCubes Instance { get; private set; }
private void Awake()
{
Instance = this;
}
void Start()
{
//for (int i = 0; i < CubesAdder.Count; i++)
//{
// transfPos.Add(CubesAdder[i].transform.position);
//}
}
void Update()
{
if (Input.GetKeyDown("v"))
{
}
if (Input.GetKeyDown("p"))
{
Time.timeScale = 3f;
}
timer -= Time.deltaTime;
if (timer <= 0)
{
StartCoroutine(TurnOffCubesAfterXSeconds());
timer = duration;
}
}
IEnumerator TurnOffCubesAfterXSeconds()
{
SpawningCubesLoop();
yield return new WaitForSeconds(1.5f);
DisablePool();
}
public void DisablePool()
{
pubInstance.SetActive(false);
}
void SpawningCubesLoop()
{
startingPos = new Vector3(ReturnPos().x, 4f, ReturnPos().z);
randText.text = startingPos.ToString();
var instanceToAdd = Instantiate(cubeSpawner[0], startingPos, Quaternion.identity); //first we create our prefab
instanceToAdd.transform.SetParent(transform); // we set the parent to be the pool just to keep track of what this things are. It's optional this one.
AddToPool(instanceToAdd); //then we call addToPool and we add that object(prefab)
var instance = availableObjects.Dequeue();
instance.SetActive(true);
pubInstance = instance;
}
Vector3 ReturnPos()
{
getVector = CubesAdder[Random.Range(0, CubesAdder.Count)].transform.position;
return getVector;
}
//void DequeTheCubesFromQueu()
//{
// var instance = availableObjects.Dequeue();
// instance.SetActive(true);
// pubInstance = instance;
//}
public void AddToPool(GameObject instance)
{
instance.SetActive(false); //first we set it to false
availableObjects.Enqueue(instance); //then we add it to our available queu
}
}
This is what is happening: