Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Please Help!

Discussion in '2D' started by Eyl_, Nov 19, 2018.

  1. Eyl_

    Eyl_

    Joined:
    Nov 19, 2018
    Posts:
    8
    Hello everyone! I have a problem on the game that i'm making. I want to instantiate prefabs that already exist and drag an can drag an drop them where I want in the scene, to make a balance game, like balancity. But when the prefab is instantiated, and when this prefab also has already been instantiated, one of the two prefabs doesn't work anymore. You can't drag and drop it, it doesn't react. This is the script I use for the prefabs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class FormInitialization : MonoBehaviour {
    7.     private Rigidbody2D rb;
    8.     private bool canSpawn;
    9.     private bool selected;
    10.     private SpriteRenderer sr;
    11.     private Vector3 initialPos;
    12.     private Collider2D coll;
    13.     private bool hasSpawn;
    14.     private int spawner;
    15.     private bool isBig;
    16.     private FormInitialization fI;
    17.     // Use this for initialization
    18.     void Start () {
    19.         fI = GetComponent<FormInitialization>();
    20.         isBig = false;
    21.         transform.localScale = transform.localScale/2;
    22.         this.name = this.name + Time.deltaTime + GetInstanceID();
    23.         fI.name = fI.name + this.name;
    24.         hasSpawn = false;
    25.         initialPos = transform.position;
    26.         rb = GetComponent<Rigidbody2D>();
    27.         rb.gravityScale = 0;
    28.         canSpawn = false;
    29.         sr = GetComponent<SpriteRenderer>();
    30.         coll = GetComponent<PolygonCollider2D>();
    31.         coll.isTrigger = true;
    32.         if(transform.position.y == 3.84f){
    33.             spawner = 1;
    34.         }
    35.         if(transform.position.y == 1.92f){
    36.             spawner = 2;
    37.         }
    38.         if(transform.position.y == 0f){
    39.             spawner = 3;
    40.         }
    41.         if(transform.position.y == -1.92f){
    42.             spawner = 4;
    43.         }
    44.         if(transform.position.y == -3.84f){
    45.             spawner = 5;
    46.         }
    47.  
    48.        
    49.     }
    50.    
    51.     // Update is called once per frame
    52. void Update () {
    53.     if(hasSpawn == false){
    54.  
    55.         if (selected == true){
    56.  
    57.             Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    58.             transform.position = new Vector2(cursorPos.x, cursorPos.y);
    59.  
    60.             if(canSpawn == true){
    61.                 sr.color = new Color(0.3f,1f,0.35f,0.5f);
    62.             }
    63.  
    64.             if(canSpawn == false){
    65.                 sr.color = new Color(0.8f,0f,0f,0.5f);
    66.             }
    67.  
    68.             if(isBig == false){
    69.                 transform.localScale = transform.localScale *2;
    70.                 isBig = true;
    71.             }
    72.         }
    73.  
    74.         if(Input.GetMouseButtonUp(0) && selected == true){
    75.  
    76.             if(canSpawn == true && transform.position != initialPos){
    77.  
    78.                 rb.gravityScale = 1;
    79.                 coll.isTrigger = false;
    80.                 sr.color = new Color(1f,1f,1f,1f);
    81.                 hasSpawn = true;
    82.  
    83.                 if(spawner == 1){
    84.                     GameObject.Find("Slot1").GetComponent<slot1Script>().isThereAForm = false;
    85.                 }
    86.  
    87.                 if(spawner == 2){
    88.                     GameObject.Find("Slot2").GetComponent<slot2Script>().isThereAForm = false;
    89.                 }
    90.  
    91.                 if(spawner == 3){
    92.                     GameObject.Find("Slot3").GetComponent<slot3Script>().isThereAForm = false;
    93.                 }
    94.  
    95.                 if(spawner == 4){
    96.                     GameObject.Find("Slot4").GetComponent<slot4Script>().isThereAForm = false;
    97.                 }
    98.  
    99.                 if(spawner == 5){
    100.                     GameObject.Find("Slot5").GetComponent<slot5Script>().isThereAForm = false;
    101.                 }
    102.                
    103.             }
    104.             else{
    105.                 transform.position = initialPos;
    106.                 sr.color = new Color(1f,1f,1f,1f);
    107.                 selected = false;
    108.             }
    109.         }  
    110.         if( selected == false){
    111.             if(isBig == true){
    112.                 transform.localScale = transform.localScale/2;
    113.                 isBig = false;
    114.             }
    115.         }
    116.     }      
    117. }
    118.  
    119. void OnTriggerEnter2D(Collider2D col){
    120.     if(col.gameObject.tag == "obstacleForm"){
    121.         canSpawn = false;
    122.     }
    123.  
    124.     else{
    125.         canSpawn = true;
    126.     }
    127. }
    128.  
    129. void OnTriggerStay2D(Collider2D col){
    130.     if(col.gameObject.tag == "obstacleForm"){
    131.         canSpawn = false;
    132.     }
    133.    
    134.     else{
    135.         canSpawn = true;
    136.     }
    137. }
    138.  
    139. void OnTriggerExit2D(){
    140.     canSpawn = true;
    141. }
    142.  
    143. void OnMouseOver(){
    144.     if(Input.GetMouseButtonDown(0)){
    145.         selected = true;
    146.     }
    147. }
    148. }
    149.  
    150.  
    If someone could help me. I tried many things but no one works. Also, please be indulgent for grammar mistakes, i'm french ^^.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    No problem about the language, your mastery of barbaric languages is also very good :)
     
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    not sure, can this help ? Try to select by the holded mouse button.
    Code (CSharp):
    1. void OnMouseOver(){
    2. if (Input.GetMouseButton(0)){
    3. selected = true;
    4. }
    5. }
    If this helps, then maybe select with the raycast can be better as OnMouseOver.
     
  4. Eyl_

    Eyl_

    Joined:
    Nov 19, 2018
    Posts:
    8
    Ok i will try thank you
     
  5. Eyl_

    Eyl_

    Joined:
    Nov 19, 2018
    Posts:
    8
    Don't work :(
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    public int spawner; set the bool to public, start the game and check in the objects inspector (object which you are trying to select) if the bool is working, when you are clicking on the object:

    and try this code for testing.
    Code (CSharp):
    1. void Update () {
    2. if (Input.GetMouseButtonDown(0))
    3.         {
    4.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward, 100f);
    5.             if (hit.collider != null)
    6.             {
    7.                 if (hit.collider == col)
    8.                 {
    9.                     selected = true;
    10.                 }
    11.             }
    12.         }
    13. if(hasSpawn == false){
    14. if (selected == true){
    15. //here continue your script
     
  7. Eyl_

    Eyl_

    Joined:
    Nov 19, 2018
    Posts:
    8
    ok thank you
     
  8. Eyl_

    Eyl_

    Joined:
    Nov 19, 2018
    Posts:
    8
    I think i've found the problem : forms that doesn't reacts doesn't detect the mouse input.
     
  9. Eyl_

    Eyl_

    Joined:
    Nov 19, 2018
    Posts:
    8
    I finnaly found. The spawners and the forms were at the smae z position and form some reason sometimes spawner collider was in front of the form, so you could not access to it, because of the spaqner collider. So it works now! Thanks for your help!