Search Unity

<SOLVED> [2D, C#] Problem Setting a Value via Scripting

Discussion in 'Scripting' started by Benjamin-Sims, Mar 6, 2015.

  1. Benjamin-Sims

    Benjamin-Sims

    Joined:
    Mar 6, 2015
    Posts:
    1
    Hello, I'm having a bit of trouble (obviously), but as you can see in this picture, here, is not being saved to my player prefab, and is acting as a temp value. The script works fine, but after one respawn, the new players are getting the respawn script attached to the Player Collision.

    Here's my respawn script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class respawn : MonoBehaviour {
    5.    
    6.     public bool dead;
    7.     public GameObject Player;
    8.     public playerCollision pC;
    9.  
    10.     public static int lives = 3;
    11.    
    12.     private static Vector2 spawnPoint = new Vector2(0, -3);
    13.    
    14.     public void death () {
    15.         dead = true;
    16.         spawner.spawn = false;
    17.         lives--;
    18.  
    19.         Instantiate(Player, (Vector2)spawnPoint, Quaternion.identity);
    20.         dead = false;
    21.         spawner.spawn = true;
    22.         pC = gameObject.GetComponent<playerCollision>();
    23.         // pC.respawn = respawn;
    24.     }
    25. }
    If you could, please help.
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Wait, I don't get what your issue is currently.

    Is the problem that the Respawn Script is NOT attached to a respawned player, or is the issue that it IS attached?

    Also, why destroy the player? Why not just set the gameobject inactive, and respawn it at the position that you want? That would be a lot less costly for the system, and makes it easier (in my opinion) to handle scripts and other components.