Unity Need help It gives me a wrong position but i dont know why?

TheUninvited

New Coder
Jul 6, 2019
25
4
3
c#
It gives me a wrong position but i dont know why? what am i doing wrong and should i do instead?

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:
 

William

Active Coder
Sep 7, 2019
178
32
38
Now, I'm not a Unity/C# Expert. I have little experience with it so unfortunately, I cannot help with your actual problem but here are some things that I would like to point out with the Code:
  • Censored-Code: You've censored some of your Code out from the Program. What I mean is that you've put // before some of the Code, causing it to be censored and not run during the Program's Runtime. Out of curiosity, what was the Censored meant to do?
  • Big-Gaps in your If-Statements + Unnecessary Blank-Lines: In the If-Statement in cubesScript.cs, there is a big giant gap inside of it. Why is that? Do you plan on adding Code inside of that If-Statement or is it just unnecessary Blank-Lines? There are also a lot of unnecessary Blank-Lines inside of both Scripts. Hell, some of your Methods such as void Update() have nothing in them. They're just empty. If you could get rid of the Blank-Lines and giant gaps then that'd be nice and would help improve your Code. It also shortens the Lines of Code which means you won't have to do as much scrolling when Debugging.
Again, sorry that I couldn't help. But, I hope my tips for how to improve the Code help. If you have much better Code, then your Debugging-Process can be a lot easier. As for Censored-Code, make sure you either remove the // and find a use for that Code or get rid of the Censored-Code and don't use it at all. The big gaps and Blank-Lines also need to be removed as they're unnecessary and you're just adding more Lines for you to scroll through which could make Debugging and what you're looking for, a bit longer.
 

TheUninvited

New Coder
Jul 6, 2019
25
4
3
c#
Now, I'm not a Unity/C# Expert. I have little experience with it so unfortunately, I cannot help with your actual problem but here are some things that I would like to point out with the Code:
  • Censored-Code: You've censored some of your Code out from the Program. What I mean is that you've put // before some of the Code, causing it to be censored and not run during the Program's Runtime. Out of curiosity, what was the Censored meant to do?
  • Big-Gaps in your If-Statements + Unnecessary Blank-Lines: In the If-Statement in cubesScript.cs, there is a big giant gap inside of it. Why is that? Do you plan on adding Code inside of that If-Statement or is it just unnecessary Blank-Lines? There are also a lot of unnecessary Blank-Lines inside of both Scripts. Hell, some of your Methods such as void Update() have nothing in them. They're just empty. If you could get rid of the Blank-Lines and giant gaps then that'd be nice and would help improve your Code. It also shortens the Lines of Code which means you won't have to do as much scrolling when Debugging.
Again, sorry that I couldn't help. But, I hope my tips for how to improve the Code help. If you have much better Code, then your Debugging-Process can be a lot easier. As for Censored-Code, make sure you either remove the // and find a use for that Code or get rid of the Censored-Code and don't use it at all. The big gaps and Blank-Lines also need to be removed as they're unnecessary and you're just adding more Lines for you to scroll through which could make Debugging and what you're looking for, a bit longer.
i know right haha but this is just all testing when i am finishing with testing i just remove them ;p