Home > Software design >  error 0103 using a switch statement with cross script strings in Unity (C#)
error 0103 using a switch statement with cross script strings in Unity (C#)

Time:05-05

what im trying to do is test a public string named enemyBehaviorStatus that exists on another script in this function, and swap the material accordingly. I can reference the function on the other script, but when i try to make the switch statement test the enemyBehaviorStatus it gives me error 0103: name 'enemyBehaviorStatus' does not exist on line 28 (switch (enemyBehaviorStatus))

Thank you for the help much appreciated

this is the colorchanging script

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

public class ChangeMaterialColor : MonoBehaviour
{
    
    public GameObject enemy;
    public Material Material1;
    public Material Material2;
    public Material Material3;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    
    public void swapMaterial()
    {
        switch (enemyBehaviorStatus)
        {
            case "passive":      
        enemy.GetComponent<MeshRenderer> ().material = Material1;
            break;
            
            case "agressive":      
        enemy.GetComponent<MeshRenderer> ().material = Material2;
            break;
            
            case "wounded":      
        enemy.GetComponent<MeshRenderer> ().material = Material3;
            break;
            
            default:       
        enemy.GetComponent<MeshRenderer> ().material = Material1;
            break;      
        }
    }
}

this is the enemy behavior script with enemyBehaviorStatus in it

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

public class EnemyNavAI : MonoBehaviour
{
    
    public Transform agentGoal;
    private NavMeshAgent nmAgent;
    public string enemyBehaviorStatus = "passive";
    
    
    // Start is called before the first frame update
    void Start()
    {
        nmAgent = GetComponent<NavMeshAgent>();
        this.GetComponent<ChangeMaterialColor>().swapMaterial();
        //nmAgent.destination = agentGoal.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (enemyBehaviorStatus == "agressive")
        {
             nmAgent.destination = agentGoal.position;
             this.GetComponent<ChangeMaterialColor>().swapMaterial();
        } 
        
        if (enemyBehaviorStatus == "wounded")
        {
            //nmAgent.destination = agentGoal.position - (gameObject.tag == "Player").position;
            this.GetComponent<ChangeMaterialColor>().swapMaterial();
        }
    }
    
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
         {
        nmAgent.destination = agentGoal.position;
        enemyBehaviorStatus = "agressive";
        this.GetComponent<ChangeMaterialColor>().swapMaterial();
         }
    }
}

CodePudding user response:

The swapMaterial() method needs to take in the string as a parameter unless enemyBahaviorStatus is a class property.

So essentially swapMaterial(string enemyBahaviourStatus) {...}

OR

Public string enemyBahaviourStatus; (and a value is assigned somewhere else in your code).

Hope this helps I wasn't 100% sure if I understood your question.

CodePudding user response:

I dont know if this is the best method, but i did fix it by making a string called enemyStatus in the color changing script and assigning it to equal enemy.GetComponent().enemyBehaviorStatus;. I then just inserted enemyStatus into the switch testing () and it works as intented. Colors change when enemyBehaviorStatus changes.

  •  Tags:  
  • c#
  • Related