error CS0176: Static member `AIPerson1.SpeedWalk' cannot be accessed with an instance reference, qua

Help erro: error CS0176: Static member `AIPerson1.SpeedWalk’ cannot be accessed with an instance reference, qualify it with a type name instead

using UnityEngine;
using System.Collections;

public class IntelligentSystem1 : MonoBehaviour {

    public GameObject Person;
    public float SpeedWalk=2;
    private int x;
    public float SpeedRotate = 8;

    void  OnTriggerEnter ( Collider other  ){
        AIPerson1 sv = Person.GetComponent<AIPerson1>();
        sv.SpeedWalk=0;
        x=1;
    }

    void  Update (){
        if(x==1)
        {
            AIline();
        }
    }

    IEnumerator AIline (){
        AIPerson1 sv = Person.GetComponent<AIPerson1>();
        sv.SpeedWalk=0;
        Person.transform.rotation = Quaternion.Slerp(Person.transform.rotation, Quaternion.LookRotation(Person.transform.position + Vector3(-6, -100, 10)), SpeedRotate * Time.deltaTime);
        Person.transform.position = Vector3.MoveTowards(Person.transform.position, (Person.transform.position + Vector3(-6, 0, 10)), Time.deltaTime * SpeedWalk);   
        yield return new WaitForSeconds(1);
        Person.transform.position = Vector3.MoveTowards(Person.transform.position, (Person.transform.position + Vector3(6, 0, 0)), Time.deltaTime * SpeedWalk);   
        yield return new WaitForSeconds(1);
        sv.SpeedWalk=1;
        x=0;
        yield return new WaitForSeconds(1);
        sv.SpeedWalk=3;
    }
}

Does the AIPerson1 class have a SpeedWalk field?

using UnityEngine;
using System.Collections;

public class AIPerson1 : MonoBehaviour {
public Transform[ ] points;
public static int SpeedWalk = 2;
public Transform person;
public int currentpoint;
public GameObject intelligentsystem;
public GameObject pdead;
public GameObject panimation;
public float SpeedRotate = 15;
public int crash;

void Update (){
transform.position = Vector3.MoveTowards(transform.position, points[currentpoint].position, Time.deltaTime * SpeedWalk);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(points[currentpoint].position - transform.position), SpeedRotate * Time.deltaTime);
}

void OnCollisionEnter ( Collision collision ){
//CONTACT WITH OBJECT
if (collision.relativeVelocity.magnitude > 4)
{
intelligentsystem.SetActiveRecursively(false);
person.GetComponent().isTrigger=true;
pdead.SetActiveRecursively(true);
panimation.SetActiveRecursively(false);
SpeedWalk=0;
}
}

void OnTriggerEnter ( Collider other ){
//POINTS CORRECTLY LINE
if(other.gameObject.tag == “AIPersonPoint”)
{
if(currentpoint<9)
{
currentpoint++;
}

if(currentpoint==9)
{
currentpoint=0;
}
}
}
}

A static member (like SpeedWalk in your second class - “public static”) is a part of the class as a concept, not a part of each instance of the class. A static member would be shared among all copies of AIPerson1 you ever spawn. To access SpeedWalk as it is, you’d use AIPerson1.SpeedWalk.

However, I’m fairly sure it shouldn’t be static, as it appears to be conceptually linked to each individual person. So just remove the “static” part of its declaration.

There are other issues, too. You’re not using coroutines correctly (your IEnumerator AIline function). It looks like "x"s only function is to trigger that coroutine, but that should be done directly through StartCoroutine.

void  OnTriggerEnter ( Collider other  ){
        AIPerson1 sv = Person.GetComponent<AIPerson1>();
        sv.SpeedWalk=0;
        StartCoroutine(AIline());
    }
//REMOVE your Update function entirely

In general, if you’re calling a coroutine from Update, you’re probably doing it wrong. A coroutine only needs to be started up once, and then it runs until its code is finished - you never need to go back to it from Update.

And coroutines are always called using StartCoroutine.

1 Like

Thanks man, save me again. :wink: