Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script is working on only one prefab

Discussion in '2D' started by liron7414, Jan 28, 2019.

  1. liron7414

    liron7414

    Joined:
    Jan 28, 2019
    Posts:
    4
    I am trying to make a copy of a Dont touch the spike game for practice purposes.
    theres the script that attached to the "spike"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpikeAnim : MonoBehaviour
    6. {
    7.     public static SpikeAnim instance;
    8.     public Animator anim;
    9.     public bool baba;
    10.     public int x;
    11.     public int K;
    12.     Rigidbody2D rb;
    13.     public int The;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         baba = false;
    19.         if (instance == null)
    20.         {
    21.             instance = this;
    22.         }
    23.         K = 10;
    24.     }
    25.  
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         rb = GetComponent<Rigidbody2D>();
    31.  
    32.         x = 1;
    33.         if (x == 1)
    34.         {
    35.             K = 3;
    36.         }
    37.         if (baba == true)
    38.         {
    39.             anim.SetBool("SpikeB", true);
    40.         }
    41.         if (The == 2)
    42.         {
    43.             anim.SetBool("SpikeB", true);
    44.         }
    45.        
    46.         anim = GetComponent<Animator>();
    47.     }
    48.     public void SpikeBb()
    49.     {
    50.         baba = true;
    51.         //anim.SetBool("SpikeB",true);          
    52.         InvokeRepeating("dada", 1f, 100000f);
    53.         K = 3;
    54.  
    55.        
    56.     }
    57.     void dada()
    58.     {
    59.  
    60.             CancelInvoke("dada");
    61.        
    62.     }
    63.     public void SpikeDd()
    64.     {
    65.  
    66.  
    67.     }
    68.     public void PleaseKillMe()
    69.     {
    70.         Destroy(GameObject.FindWithTag("Spike"));
    71.     }
    72.     void OnTriggerEnter2D(Collider2D col)
    73.     {
    74.         if(col.gameObject.tag == "Remover")
    75.         {
    76.             Destroy(gameObject);
    77.         }
    78.     }
    79.     public void Des()
    80.     {
    81.         if (x == 1)
    82.         {
    83.  
    84.             Destroy(GameObject.FindWithTag("Spike"));
    85.  
    86.  
    87.         }
    88.            
    89.     }
    90. }
    91. thats the script that controlles the ball
    92.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallController : MonoBehaviour
    6. {
    7.     public static BallController instance;
    8.  
    9.     public SpikeAnim sa;
    10.     Rigidbody2D rb;
    11.     public bool started;
    12.     public int speed;
    13.     public int jump;
    14.     int x;
    15.     public GameObject ball;
    16.     public Transform t;
    17.    
    18.     // Start is called before the first frame update
    19.  
    20.     void Start()
    21.     {
    22.         //sa = GetComponent<SpikeAnim>();
    23.         rb = GetComponent<Rigidbody2D>();
    24.         started = false;
    25.         x = 1;
    26.         if (instance == null)
    27.         {
    28.             instance = this;
    29.         }
    30.      
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.  
    37.  
    38.         if (!started)
    39.         {
    40.             if (Input.GetMouseButtonDown(0))
    41.             {
    42.                 rb.simulated = true;
    43.                 started = true;
    44.             }
    45.  
    46.         }
    47.         if (started)
    48.         {
    49.             if (Input.GetMouseButtonDown(0))
    50.             {
    51.                 if (x == 1)
    52.                 {
    53.                     rb.velocity = new Vector2(speed, jump);
    54.                 }
    55.                 if(x == 2)
    56.                 {
    57.                     rb.velocity = new Vector2(-speed, jump);
    58.                 }
    59.  
    60.  
    61.             }
    62.  
    63.         }
    64.  
    65.     }
    66.  
    67.  
    68.     void OnTriggerEnter2D(Collider2D col)
    69.     {
    70.         if (col.gameObject.tag == "Wall")
    71.         {
    72.             rb.velocity = new Vector2(-speed, jump-1);
    73.             x = 2;
    74.             SpikeAnim.instance.The = 2;
    75.  
    76.             //SpikeSpawner.instance.IDontKnow();
    77.  
    78.             //SpikeAnim.instance.SpikeBb();
    79.             //SpikeSpawner.instance.StopSpawning();
    80.             //JustKillMe();
    81.             //SpikeSpawner.instance.SpawnLeftSide();
    82.             //SpikeAnim.instance.PleaseKillMe();
    83.            
    84.             //if (sa.baba == false)
    85.             //{
    86.             //    sa.baba = true;
    87.             //}
    88.            
    89.         }
    90.         if (col.gameObject.tag == "Wall1")
    91.         {
    92.             rb.velocity = new Vector2(speed, jump);
    93.             x = 1;
    94.             //SpikeSpawner.instance.SpawnRightSide();
    95.  
    96.  
    97.         }
    98.         if(col.gameObject.tag == "Spike")
    99.         {
    100.             Destroy(gameObject);
    101.         }
    102.  
    103.    
    104.     }
    105.    
    106.  
    107. }  
    108.  
    109.  
    110.  
     

    Attached Files:

  2. liron7414

    liron7414

    Joined:
    Jan 28, 2019
    Posts:
    4
    ignore my rage voids
     
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Can you explain what is supposed to happen, and what actually happends ?
     
  4. liron7414

    liron7414

    Joined:
    Jan 28, 2019
    Posts:
    4
    3 of the cubes must go back
    only one of them
     
  5. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    I guess it's because of the static field. If you have a classe called Spike, with a static field in it, the static field will be shared amongst all instances of the Spike class.

    Honestly, your entire scripts uses weird field names, weird method names, and i don't understand what you are actually trying to do.

    If you explained to me step by step what is your script supposed to do, i could try to create an example script for you.
     
  6. liron7414

    liron7414

    Joined:
    Jan 28, 2019
    Posts:
    4
    never mind i succeded
     
  7. shreyasm023

    shreyasm023

    Joined:
    Jul 10, 2021
    Posts:
    4
    how you have done it , same problem!