简体   繁体   English

如何引用不同类C#的非静态成员

[英]How do I reference a non-static member of a different class c#

I am trying to make a game where my background scrolls depending on how fast I want the player to be going. 我正在尝试制作一个游戏,其中我的背景会根据我希望玩家前进的速度而滚动。

I have tried creating a non-static function, that accesses BackgroundScroller.speed as a simple way to pass the value. 我尝试创建一个非静态函数,该函数将BackgroundScroller.speed作为传递值的一种简单方法。

.

(PlayerController.cs) (PlayerController.cs)

void Setspeed(float setSpeed){

BackgroundScroller.speed = setSpeed;

}

BackgroundScroller.cs looks like this: BackgroundScroller.cs看起来像这样:

using UnityEngine;
using System.Collections;

public class BackgroundScroller : MonoBehaviour {

public float speed = 0;
public static BackgroundScroller current;

float pos = 0;

void Start () {
    current = this;
}

public void Go () {
    pos += speed;
    if (pos > 1.0f)
        pos-= 1.0f;


    renderer.material.mainTextureOffset = new Vector2 (pos, 0);
}

}

.

The error I get when I try and access BackgroundScroller.speed from PlayerController.cs is: "An object reference is required to access non-static member "BackgroundScroller.speed". 我尝试从PlayerController.cs访问BackgroundScroller.speed时遇到的错误是:“访问非静态成员“ BackgroundScroller.speed”需要对象引用。

I don't understand how to access the value of BackgroundScroller.speed from PlayerController.cs essentially. 我根本不了解如何从PlayerController.cs访问BackgroundScroller.speed的值。 I don't want to create an object reference, I just want to simply change the value in the other class. 我不想创建对象引用,我只想简单地更改其他类中的值。

Cheers 干杯

Lucio 路西欧

You cannot statically access speed because it is not a static member. 您不能静态访问speed因为它不是静态成员。 It is an instance variable that can only be accessed through an instantiated BackgroundScroller. 它是一个实例变量,只能通过实例化的BackgroundScroller进行访问。

Assuming that Start has already been called somewhere ensuring that BackgroundScroller.current is not null, the following line will give you access to the speed to use the existing static reference for the current scroller. 假设已经在某个位置调用了Start并确保BackgroundScroller.current不为null,则以下行将使您能够访问将当前静态引用用于当前滚动器的速度。

BackgroundScroller.current.speed = setSpeed;

Because the speed is not static type and you can fix this by adding static in speed variable. 由于speed不是静态类型,因此可以通过在speed变量中添加静态来解决此问题。

Try to change your speed type to static float , for example 尝试将速度类型更改为static float ,例如

public static float speed;

then you can finally set the value of speed 然后您最终可以设置speed

void Setspeed(float setSpeed){
    BackgroundScroller.speed = setSpeed;
}

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

相关问题 C#“非静态字段需要对象引用”,静态成员函数的类问题 - C# “An object reference is required for the non-static field,” Class issue with Static Member Function C#如何在非静态成员函数上调用Action? - C# How do I invoke an Action on a non-static member function? C ++ Interop:如何从本机C ++调用C#类,这个类是非静态的? - C++ Interop: How do I call a C# class from native C++, with the twist the class is non-static? 如何从其他类的静态方法中调用非静态方法? - How do I call a non-static method from a static method from a different class? 如何从 C# 中的静态方法调用非静态方法? - How do I call a non-static method from a static method in C#? 在C#中编写静态和非静态方法时,如何避免出现“调用不明确……”错误? - How do I avoid 'call is ambiguous…' error when writing static and non-static methods in C#? Unity需要对象引用才能访问非静态成员C# - Unity An object reference is required to access non-static member C# 如何在C#中的另一个类之外调用非静态方法? - How can I call a non-static method out of another class in C#? 无法从C#中的嵌套类访问非静态成员 - Can not access a non-static member from nested class in C# 如何修复“访问非静态成员需要对象引用” - How to fix 'An object reference is required to acces non-static member'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM