Search Unity

Problem with startPos

Discussion in 'Scripting' started by unity_xAnmDpLX9JUzqA, Jan 2, 2020.

  1. unity_xAnmDpLX9JUzqA

    unity_xAnmDpLX9JUzqA

    Joined:
    Dec 25, 2018
    Posts:
    4
    Hi there
    I'm still a beginner in unity and need help. My problem is I want to restart my objects when they collide with another object but it only restarts one object. The CharacterCreation is there for me that I can change the characters at the button. My problem is that the characters are all in a different position when it comes to the start position. Here are my scripts.
    Sorry for my bad english
    Thx

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class BallController : MonoBehaviour
    {
    public Rigidbody rb;
    private Vector3 startPos;
    public float impulseForce = 5f;

    // Use this for initialization
    void Awake()
    {
    startPos = transform.position;
    }

    private void OnCollisionEnter(Collision other)

    {
    if (!other.transform.GetComponent<Goal>())
    {
    /*foreach (Transform t in other.transform.parent)
    {
    gameObject.AddComponent<TriangleExplosion>();

    StartCoroutine(gameObject.GetComponent<TriangleExplosion>().SplitMesh(true));
    //Destroy(other.gameObject);
    Debug.Log("exploding - exploding - exploding - exploding");
    }*/

    }



    {
    DeathPart deathPart = other.transform.GetComponent<DeathPart>();
    if (deathPart)
    deathPart.HittedDeathPart();
    }





    rb.velocity = Vector3.zero;
    rb.AddForce(Vector3.up, ForceMode.Impulse);

    }

    public void ResetBall() {
    transform.position = startPos;
    }
    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameManager : MonoBehaviour {

    public static GameManager singleton;
    public int best;
    public int score;


    private void Awake()
    {
    if (singleton == null)
    singleton = this;
    else if (singleton != this)
    Destroy(gameObject);

    // Load the saved highscore
    best = PlayerPrefs.GetInt("Highscore");
    }

    public void NextLevel()
    {
    FindObjectOfType<BallController>().ResetBall();

    }

    public void RestartLevel()
    {
    Debug.Log("Restarting Level");
    // Show Adds Advertisement.Show();
    singleton.score = 0;
    FindObjectOfType<BallController>().ResetBall();
    //Reload the Stage
    }

    public void AddScore(int scoreToAdd)
    {
    score += scoreToAdd;

    if (score > best)
    {
    best = score;
    PlayerPrefs.SetInt("Highscore", score);
    }
    }


    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DeathPart : MonoBehaviour {

    private void OnEnable()
    {
    GetComponent<Renderer>().material.color = Color.black;


    }

    public void HittedDeathPart( )
    {
    GameManager.singleton.RestartLevel();
    }
    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CharacterCreation : MonoBehaviour
    {
    private List<GameObject> models;
    //Default Index of the model
    private int selectionIndex = 0;

    private void Start()
    {
    models = new List<GameObject>();
    foreach(Transform t in transform)
    {
    models.Add(t.gameObject);
    t.gameObject.SetActive(false);
    }
    models[selectionIndex].SetActive(true);
    }

    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.A))
    Select(4);
    }

    public void Select (int index)
    {
    if (index == selectionIndex)
    return;

    if (index < 0 || index >= models.Count)
    return;

    models[selectionIndex].SetActive(false);
    selectionIndex = index;
    models[selectionIndex].SetActive(true);
    }

    }
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    "My problem is I want to restart my objects when they collide with another object but it only restarts one object."
    I really don't understand your problem.
    Try to explain your game idea and maybe we can help you.

    If it's only about a simple collision and reset all the objects.. then you have 2 options (maybe more)
    1. Easiest, i don't recommend it (works for small applications) -> restart your level when your object collide. If your game starts in 20 seconds this is not an option :)
    2. Make a method that reinitialize all the objects. Before that, maybe you need to store all the positions/rotations etc.. of all your objects and then just reactivate and move/rotate your objects in the positions saved before.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847