简体   繁体   中英

Unity3D: Error with my code

I'm getting an error on a code of mine, and was wondering if anyone can help me with this, I search up the problem and tried out the soultion but nothing seemed to work. (what I tried/searched up: move my c# script linked up to the script below in the standard assets folder) Thank you!

This is the error I am getting:

The name 'GameManager' does not denote a valid type ('not found')

And this is the code

EDIT

 var thisMaterial : Material;

 private var gameManager : GameManager;
 private var moveDirection : Vector2;
 private var movePosition : Vector2;
 private var lastDirection : int;

  function Start()
{
   gameManager = (GameManager)FindObjectOfType(typeof(GameManager));
    thisMaterial = new Material(Shader.Find("Unlit/Transparent"));
    GetComponent("MeshRenderer").material = thisMaterial;
    thisMaterial.mainTexture = gameManager.game.characters[0].spriteSheet;
    thisMaterial.mainTextureOffset = gameManager.animationList[0][0];
    movePosition = gameManager.game.startPosition;
 }

   function Update()
 {
    var vectorMovePosition : Vector3 = Vector3(movePosition.x, movePosition.y, transform.position.z);
    var distanceToVector : float = Vector3.Distance(transform.position, vectorMovePosition);
    var inputDirection : Vector2 = Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
    if(inputDirection != Vector2.zero && distanceToVector < 0.1)
    {
        if(inputDirection == -Vector2.up) lastDirection = 0;
        else if(inputDirection == -Vector2.right) lastDirection = 1;
        else if(inputDirection == Vector2.right) lastDirection = 2;
        else if(inputDirection == Vector2.up) lastDirection = 3;
        else return;

         thisMaterial.mainTextureOffset = gameManager.animationList[lastDirection][0];
        if(gameManager.game.scenes[gameManager.currentScene].GetTile(3, movePosition + inputDirection) == Vector2(-1, -1))
        {
            moveDirection = inputDirection;
            movePosition = vectorMovePosition + moveDirection;
        }
    }
    if(transform.position != vectorMovePosition)
    {
        thisMaterial.mainTextureOffset = gameManager.animationList[lastDirection][Mathf.Round(distanceToVector * 3)];
        transform.position = Vector3.MoveTowards(transform.position, vectorMovePosition, Time.deltaTime * 3);
    }
 }

This line is your problem.

gameManager = FindObjectOfType(GameManager);

This should be like this:

gameManager = (GameManager)FindObjectOfType(typeof(GameManager));

Alternatives:

If that gameobject is not the same as the one that the above script is on:

gameManager = GameObject.FindWithTag("SomeTag").GetComponent<GameManager>();

If it is on the same gameobject, this will do:

gameManager = GetComponent<GameManager>();

Or using the inspector to assign the variable.

Of course, if all of that doesn't work, you don't actually have GameManager script attached to something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM