Question by
Matelevi12 · Jun 06, 2019 at 08:29 PM ·
parent-childrestart game
Parent and Child objects after Restart the game
So my probelm is the Staff whitch is a Child object of the Player object. Its work fine its flip if i flip my Player, BUT after i restart the game while the game is runing a Staff is wont work properly. I really need help i spent 1 day to try to fix that. I just dont get it why its work when i start the game and not if i restart.
This is when i start the game and
This is when i restart. My code looks like this rigth now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
private Vector2 moveVelocity;
Animator anim;
int move = Animator.StringToHash("Move");
public GameObject staff;
public SpriteRenderer StaffSprite;
public Vector3 minCameraPos;
public Vector3 maxCameraPos;
public bool bounds;
//UI
public int HP = 5;
public TextMeshProUGUI Hptext;
public int Money = 0;
public TextMeshProUGUI MoneyText;
//Dead
public GameObject DeadScreen;
public bool PlayerIsDead = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
transform.parent = staff.transform;
}
private void Reset()
{
Time.timeScale = 1;
PlayerIsDead = false;
SceneManager.LoadScene(1);
// SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
// Update is called once per frame
void Update()
{
//UI
Hptext.text = "HP: " + HP;
if(HP == 0)
{
Dead();
DeadScreen.SetActive(true);
PlayerIsDead = true;
if (Input.GetKeyDown(KeyCode.R) && PlayerIsDead == true)
{
Reset();
}
}
MoneyText.text = ""+Money;
//Movement
Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
moveVelocity = moveInput.normalized * speed;
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (pos.x < gameObject.transform.position.x)
{
staff.transform.Rotate(new Vector3(180, 0, 0));
Debug.Log("Left");
//StaffSprite.flipY = true;
transform.eulerAngles = new Vector2(0, 180);
//StaffSprite.transform.Rotate(new Vector2(180, 0));
staff.transform.localPosition = new Vector3(0.0675f, -0.065f, -1f);
}
else
{
Debug.Log("Right");
transform.eulerAngles = new Vector2(0, 0);
//StaffSprite.transform.Rotate(new Vector2(0, 0));
//StaffSprite.flipY = false;
staff.transform.localPosition = new Vector3(0.0675f, -0.065f, 1f);
}
if (bounds)
{
transform.position = new Vector2(Mathf.Clamp(transform.position.x, minCameraPos.x, maxCameraPos.x),
Mathf.Clamp(transform.position.y, minCameraPos.y, maxCameraPos.y));
}
}
void Dead()
{
Time.timeScale = 0;
GameObject.Destroy(staff);
//gameObject.SetActive(false);
gameObject.GetComponent<Renderer>().enabled = false;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
}
void RotateLeft()
{
transform.Rotate(Vector3.down * -180);
}
void RotateRight()
{
transform.Rotate(Vector3.down * 180);
}
void OnTriggerEnter2D(Collider2D Coin)
{
if (Coin.tag == "Coin")
{
Money += 1;
Debug.Log("Coin picked up");
Destroy(Coin.gameObject);
}
}
}
Comment
Hello.
Why all that empty lines?... I edited a little your post deleting soooo many empty lines... Please, if posting code, do it easy for us to read... delete all unnecessary lines please...