Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Issue with connecting games 2 scenes to bring the selected character to 2d scene resulted error code

Discussion in 'Scripting' started by unity_user0007, May 26, 2024.

  1. unity_user0007

    unity_user0007

    Joined:
    May 19, 2024
    Posts:
    16
    I have developed 2 scenes:
    1st scene: character selection that works fines alone
    2nd scene: actual 2D game that works fine without connecting to 1st scene

    I've made a code that connects the character selection scene to game scene with selected character. Then I tried to combine this code to initial Player code on 2nd scene but I get error msg:
    Assets\Player_test.cs(54,18): error CS0111: Type 'Player_test' already defines a member called 'Start' with the same parameter types

    It seems like when the 2 codes are combined, then the starts modules must be merged or renamed as upate but don't know how to do it. Basically I want the selected character from 1st scene to appear in 2md game scene and the game will proceed with that character.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_test : MonoBehaviour
    6. {
    7.     public CharacterDatabase characterDB;
    8.  
    9.     public SpriteRenderer artworkSprite;
    10.  
    11.     public Sprite[] sprites;
    12.     public float strength = 5f;
    13.     public float gravity = -9.81f;
    14.     public float tilt = 5f;
    15.  
    16.     private SpriteRenderer spriteRenderer;
    17.     private Vector3 direction;
    18.     private int spriteIndex;
    19.    
    20.     private int selectedOption = 0;
    21.     // Start is called before the first frame update
    22.  
    23. void Start()
    24.     {
    25.         if(!PlayerPrefs.HasKey("selectedOption"))
    26.         {
    27.            selectedOption = 0;
    28.         }
    29.         else{
    30.             Load();
    31.         }
    32.         UpdateCharacter(selectedOption);
    33.        
    34.     }
    35.  
    36.     private void UpdateCharacter(int selectedOption)
    37.     {
    38.         Character character = characterDB.GetCharacter(selectedOption);
    39.         artworkSprite.sprite = character.characterSprite;
    40.     }
    41.  
    42.     private void Load()
    43.     {
    44.         selectedOption = PlayerPrefs.GetInt("selectedOption");
    45.  
    46.     }
    47.  
    48.    
    49.     private void Awake()
    50.     {
    51.         spriteRenderer = GetComponent<SpriteRenderer>();
    52.     }
    53.  
    54.     private void Start()
    55.     {
    56.         InvokeRepeating(nameof(AnimateSprite), 0.15f, 0.15f);
    57.     }
    58.  
    59.     private void OnEnable()
    60.     {
    61.         Vector3 position = transform.position;
    62.         position.y = 0f;
    63.         transform.position = position;
    64.         direction = Vector3.zero;
    65.     }
    66.  
    67.     private void Update()
    68.     {
    69.         if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
    70.             direction = Vector3.up * strength;
    71.         }
    72.  
    73.         if (Input.touchCount > 0)
    74.         {
    75.             Touch touch = Input.GetTouch(0);
    76.             if (touch.phase == TouchPhase.Began) {
    77.                 direction = Vector3.up * strength;
    78.             }
    79.         }
    80.  
    81.         // Apply gravity and update the position
    82.         direction.y += gravity * Time.deltaTime;
    83.         transform.position += direction * Time.deltaTime;
    84.  
    85.         // Tilt the bird based on the direction
    86.         Vector3 rotation = transform.eulerAngles;
    87.         rotation.z = direction.y * tilt;
    88.         transform.eulerAngles = rotation;
    89.     }
    90.  
    91.     private void AnimateSprite()
    92.     {
    93.         spriteIndex++;
    94.  
    95.         if (spriteIndex >= sprites.Length) {
    96.             spriteIndex = 0;
    97.         }
    98.  
    99.         if (spriteIndex < sprites.Length && spriteIndex >= 0) {
    100.             spriteRenderer.sprite = sprites[spriteIndex];
    101.         }
    102.     }
    103.  
    104.     private void OnTriggerEnter2D(Collider2D other)
    105.     {
    106.         if (other.gameObject.CompareTag("Obstacle")) {
    107.             GameManager.Instance.GameOver();
    108.         } else if (other.gameObject.CompareTag("Scoring")) {
    109.             GameManager.Instance.IncreaseScore();
    110.         }
    111.     }
    112.  
    113. }
    114.  
    115.  
    116.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
    Not sure what all you mean by "connected to scene"

    Scenes don't really connect. They're documents that contain GameObjects, that's it.

    Generally to share information across your game you want some kind of GameManager construct.

    This GameManager will contain the state that needs to be shared by more than one scene.

    Look on Youtube for discussions about the basics of a GameManager.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687
    You have two
    Start
    methods in the same script. Are you trying to combine the code of two scripts into one?
     
  4. unity_user0007

    unity_user0007

    Joined:
    May 19, 2024
    Posts:
    16
    I fixed the start error for now. I had tried to combine 2 codes since I want to select the character among collection of characters (character database) in character selection scene and want the game scene to remember the character from selection scene and use that as the sprite. For now I have to select character manually in game scene in Player>sprite renderer (attached picture) while in the game both the selected character from character selection scene and manually selected character in gam scene appear. How can I do it with the provided code?
     

    Attached Files:

  5. unity_user0007

    unity_user0007

    Joined:
    May 19, 2024
    Posts:
    16
    Let me rephrase my question: I'm saving the selected character in character selection scene as "
    selectedOption". How can I systematically pass the "selectedOption" to Sprite Renderer >Sprite without manual drag and drop of the sprite to Sprite renderer?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
    Again, by using a GameManager:

    1. the selection screen SETS a property / field in the Game Manager saying "player has selected X"

    2. the play screen READS that property and selects the appropriate sprite, most likely by looking it up in the same table that the first scene used.

    I keep this handy-dandy ScriptableObject around for whenever I need a particular list of things and I need it to be defined in only one place and used by multiple places:

    https://gist.github.com/kurtdekker/68b3fd9d4202888b4afc17b32eed6790