Need Help, trying to access variables from other files and pulling errors in unity.

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?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class TestLvl3 : MonoBehaviour
{
    public float speedTest = 7f;
    public float jumpingPowerTest = 19f;

    public GameObject Player1;
    public GameObject Player2;

    public float Speed1;
    public float Speed2;
    public float jumpingPower1;
    public float jumpingPower2;
    public float startPos1;
    public float startPos2;
   
    void Start()
    {
        Player1 = GameObject.Find("Player1");
        Player2 = GameObject.Find("Player2");
       

        Speed1 = Player1.GetComponent<PlayerMovement>().speed;
        jumpingPower1 = Player1.GetComponent<PlayerMovement>("jumpingPower");
        Speed2 = Player2.GetComponent<PlayerMovement>("Speed2");
        jumpingPower2 = Player2.GetComponent<PlayerMovement>("jumpingPower2");
        startPos1 = Player1.GetComponent<PlayerMovement>("startPos");
        startPos2 = Player2.GetComponent<PlayerMovement>("startPos");
        /*
        Player1.GetComponent<GameController>();
        Player2.GetComponent<GameController>();
        */
    }

   
    void Update()
    {
       
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.CompareTag("Speed"))
        {
            if (speed != speedTest)
            {
                Die();
            }
            else if (speed2 != speedTest)
            {
                Player2.Die();
            }
        }
        else if (collision.CompareTag("Jump"))
        {
            if (jumpingPower != jumpingPowerTest)
            {
                Player1.Die();
            }
            else if (jumpingPower2 != jumpingPowerTest)
            {
                Player2.Die();
            }
        }

    }

}

… 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:

https://discussions.unity.com/t/833085/2

https://discussions.unity.com/t/839310

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.

1 Like