Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

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

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:
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:
To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
 
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.
 
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
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom