简体   繁体   English

无法弄清楚如何从另一个脚本访问int

[英]Can't figure out how to access an int from another script

I know this question has been asked before, here and here . 我知道以前在这里这里都曾问过这个问题。
But I still can't get variables from another script. 但是我仍然无法从另一个脚本获取变量。 I don't know what I'm doing wrong. 我不知道我在做什么错。
*(I'm really new to programming in general so I might have missed something glaringly obvious) *(一般而言,我真的是编程新手,所以我可能错过了显而易见的东西)

I keep on getting the error: The name 'points' does not exist in the current context 我继续收到错误消息: The name 'points' does not exist in the current context

The slimespawner script is on the Canvas. slimespawner脚本位于“画布”上。

Sorry if the question is too simple. 很抱歉,这个问题太简单了。

here's the script I'm trying to access: 这是我要访问的脚本:

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

public class slimespawner : MonoBehaviour
{
    public int points;
    public Text score;
    public float xx;
    public float yy;

    void Start()
    {
        points = 0;
        xx = Random.Range(-32f, 32f);
        yy = Random.Range(-18.5f, 18.5f);
    }

    void Update()
    {
        score.text = "Score: " + points.ToString();
    }
}

And here's the script that's trying to use the points variable. 这是试图使用points变量的脚本。

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

public class slimecontroller : MonoBehaviour
{
    private float movespeed = 0.1f;
    public slimespawner slisp;

    void Start()
    {
        slisp = GameObject.Find("Canvas").GetComponent<slimespawner>();
    }
    void Update()
    {
        points += 1;
    }
}

使用slisp.points += 1;访问属性slisp.points += 1;

Your code should look as this: 您的代码应如下所示:

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

public class slimecontroller : MonoBehaviour
{
    private float movespeed = 0.1f;
    public slimespawner slisp;

    void Start()
    {
        slisp = GameObject.Find("Canvas").GetComponent<slimespawner>();
    }
    void Update()
    {
        slisp.points += 1;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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