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 Need Help, trying to access variables from other files and pulling errors in unity.

Discussion in 'Scripting' started by Austin4nier, May 16, 2024.

  1. Austin4nier


    May 9, 2024
    Errors include CS1501, CS0103, CS1061

    THIS IS A 2D GAME With 2 PLAYERS(split screen)

    The code is as follows
    what am I doing wrong?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    7. public class TestLvl3 : MonoBehaviour
    8. {
    9.     public float speedTest = 7f;
    10.     public float jumpingPowerTest = 19f;
    12.     public GameObject Player1;
    13.     public GameObject Player2;
    15.     public float Speed1;
    16.     public float Speed2;
    17.     public float jumpingPower1;
    18.     public float jumpingPower2;
    19.     public float startPos1;
    20.     public float startPos2;
    22.     void Start()
    23.     {
    24.         Player1 = GameObject.Find("Player1");
    25.         Player2 = GameObject.Find("Player2");
    28.         Speed1 = Player1.GetComponent<PlayerMovement>().speed;
    29.         jumpingPower1 = Player1.GetComponent<PlayerMovement>("jumpingPower");
    30.         Speed2 = Player2.GetComponent<PlayerMovement>("Speed2");
    31.         jumpingPower2 = Player2.GetComponent<PlayerMovement>("jumpingPower2");
    32.         startPos1 = Player1.GetComponent<PlayerMovement>("startPos");
    33.         startPos2 = Player2.GetComponent<PlayerMovement>("startPos");
    34.         /*
    35.         Player1.GetComponent<GameController>();
    36.         Player2.GetComponent<GameController>();
    37.         */
    38.     }
    41.     void Update()
    42.     {
    44.     }
    46.     private void OnTriggerEnter2D(Collider2D collision)
    47.     {
    49.         if (collision.CompareTag("Speed"))
    50.         {
    51.             if (speed != speedTest)
    52.             {
    53.                 Die();
    54.             }
    55.             else if (speed2 != speedTest)
    56.             {
    57.                 Player2.Die();
    58.             }
    59.         }
    60.         else if (collision.CompareTag("Jump"))
    61.         {
    62.             if (jumpingPower != jumpingPowerTest)
    63.             {
    64.                 Player1.Die();
    65.             }
    66.             else if (jumpingPower2 != jumpingPowerTest)
    67.             {
    68.                 Player2.Die();
    69.             }
    70.         }
    72.     }
    74. }
  2. Kurt-Dekker


    Mar 16, 2013
    ... not fixing your errors??

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
    ron-bohn likes this.