'error 0103 using a switch statement with cross script strings in Unity (C#)
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();
}
}
}
Solution 1:[1]
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).
Solution 2:[2]
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<EnemyNavAI>().enemyBehaviorStatus;
I then just inserted enemyStatus
into the switch testing () and it works as intended. Colors change when enemyBehaviorStatus
changes.
Solution 3:[3]
You need to add enemyBehaviorStatus
as a parameter to the swapMaterial
method:
public void swapMaterial(string enemyBehaviorStatus)
{
// ...
}
And then pass it every time you call swapMaterial
:
this.GetComponent<ChangeMaterialColor>().swapMaterial(enemyBehaviorStatus);
This should work, and (in my opinion) is also the preferred approach:
- If A wants B to do something that needs a value X, and A has the X, then A should pass X to B.
- In your answer you make B responsible to go look for an instance of A (assuming or hoping there is an A), and then to fetch the X from it (assuming or hoping that this A contains the X that it needs and not some other X).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeremy Caney |
Solution 2 | Jeremy Caney |
Solution 3 |