想要来自发生碰撞的对象的 script.cs 组件的变量
Want the variable from a script.cs component of an object which is collided
我想访问与 isTrigger 对象发生碰撞的对象的变量 (allowWalk)。
下面是 isTrigger 对象的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieTrack : MonoBehaviour
{
public float xPos;
public float zPos;
void Start(){
xPos = transform.position.x;
zPos = transform.position.z;
}
void Update() {
//transform.position = new Vector3(xPos, transform.position.y, zPos);
}
private void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "Zombie"){
other.GetComponent<"ZombieAI">().allowWalk = false;
this.GetComponent<BoxCollider>().enabled = false;
if(Random.Range(1,3) == 1){
xPos = Random.Range(transform.position.x + 5, transform.position.x + 10);
}else{
xPos = Random.Range(transform.position.x - 5, transform.position.x - 10);
}
if(Random.Range(1,3) == 1){
zPos = Random.Range(transform.position.z + 5, transform.position.z + 10);
}else{
zPos = Random.Range(transform.position.z - 5, transform.position.z - 10);
}
other.gameObject.GetComponent<Animator>().Play("idle");
StartCoroutine(Wait());
// other.gameObject.GetComponent<Animator>().Play("walk");
}
}
IEnumerator Wait(){
yield return new WaitForSeconds(4);
transform.position = new Vector3(xPos, transform.position.y, zPos);
this.GetComponent<BoxCollider>().enabled = true;
}
}
在 OnTriggerEnter 中 other.GetComponent<"ZombieAI">().allowWalk = false;
有不同的方式。 ZombieAI.cs 是脚本。
您可以通过 GetComponent<T>
访问组件,但您将字符串传递给泛型参数。你应该因此而出错。
other.GetComponent<ZombieAI>().allowWalk = false;
我想访问与 isTrigger 对象发生碰撞的对象的变量 (allowWalk)。 下面是 isTrigger 对象的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieTrack : MonoBehaviour
{
public float xPos;
public float zPos;
void Start(){
xPos = transform.position.x;
zPos = transform.position.z;
}
void Update() {
//transform.position = new Vector3(xPos, transform.position.y, zPos);
}
private void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "Zombie"){
other.GetComponent<"ZombieAI">().allowWalk = false;
this.GetComponent<BoxCollider>().enabled = false;
if(Random.Range(1,3) == 1){
xPos = Random.Range(transform.position.x + 5, transform.position.x + 10);
}else{
xPos = Random.Range(transform.position.x - 5, transform.position.x - 10);
}
if(Random.Range(1,3) == 1){
zPos = Random.Range(transform.position.z + 5, transform.position.z + 10);
}else{
zPos = Random.Range(transform.position.z - 5, transform.position.z - 10);
}
other.gameObject.GetComponent<Animator>().Play("idle");
StartCoroutine(Wait());
// other.gameObject.GetComponent<Animator>().Play("walk");
}
}
IEnumerator Wait(){
yield return new WaitForSeconds(4);
transform.position = new Vector3(xPos, transform.position.y, zPos);
this.GetComponent<BoxCollider>().enabled = true;
}
}
在 OnTriggerEnter 中 other.GetComponent<"ZombieAI">().allowWalk = false;
有不同的方式。 ZombieAI.cs 是脚本。
您可以通过 GetComponent<T>
访问组件,但您将字符串传递给泛型参数。你应该因此而出错。
other.GetComponent<ZombieAI>().allowWalk = false;