- Home /
I fixed it myself.
Why am I getting this error?
/Scripts/Soldier.cs(113,28): error CS1061: Type AI' does not contain a definition for
suspition' and no extension method suspition' of type
AI' could be found (are you missing a using directive or an assembly reference?) I am trying to make a script where if I enter the enemy trigger their suspition raises. In order to do this I have to access a variable from the AI script from my soldier script. Here are the scripts. /Scripts/Soldier.cs(113,28): error CS1061: Type AI' does not contain a definition for
suspition' and no extension method suspition' of type
AI' could be found (are you missing a using directive or an assembly reference?)
My soldier script below.
using UnityEngine;
using System.Collections;
public class Soldier : MonoBehaviour {
private AI ai;
public float suspitionSpeed;
public float health;
public float hitSpeed = 20.0f;
public int score;
public GUIText scoreText;
public GUIText healthText;
public GUIText dead;
private bool canPickUpHealth;
private bool isHit = false;
private bool isShot = false;
// Use this for initialization
void Start () {
SetHealthText();
SetScoreText();
dead.text = "";
canPickUpHealth = false;
score = 0;
Time.timeScale = 1;
ai = GetComponent<AI>();
}
// Update is called once per frame
void Update () {
if(health >= 100.0f)
{
health = 100.0f;
SetHealthText();
}
if(health < 0.0f)
{
health = 0.0f;
SetHealthText();
}
if(health <= 0.0f)
{
Time.timeScale = 0;
dead.text = "YOU ARE DEAD";
SetHealthText();
}
if(health == 100.0f)
{
canPickUpHealth = false;
}
else
{
canPickUpHealth = true;
}
if(isHit)
{
health -= Time.deltaTime * hitSpeed;
health = Mathf.Round(health);
SetHealthText();
}
else
{
isHit = false;
}
if(isShot)
{
hitSpeed = 25.0f;
health -= Time.deltaTime * hitSpeed;
health = Mathf.Round(health);
SetHealthText();
}
else
{
isShot = false;
hitSpeed = 20.0f;
}
if(Input.GetKeyDown (KeyCode.Space))
{
audio.Play();
}
}
void OnTriggerEnter(Collider other)
{
if( other.gameObject.tag == "Health" && Input.GetKeyUp(KeyCode.E))
{
if(canPickUpHealth)
{
health = health + 15.0f;
other.gameObject.SetActive(false);
SetHealthText();
}
}
if(other.gameObject.tag == "Enemy" && health > 0.0f)
{
isHit = true;
}
if(other.gameObject.tag == "Bullet" && health > 0.0f)
{
isShot = true;
hitSpeed = 25.0f;
}
if(other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
score = score + 10;
SetScoreText();
}
if(other.gameObject.tag == "Suspicion")
{
ai.suspition += Time.deltaTime * suspitionSpeed;
}
}
void OnTriggerStay(Collider other)
{
//if(other.gameObject.tag == "Enemy")
//{
// isHit = false;
if( other.gameObject.tag == "Health" && Input.GetKeyUp(KeyCode.E))
{
if(canPickUpHealth)
{
health = health + 15.0f;
other.gameObject.SetActive(false);
SetHealthText();
audio.Play();
}
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
isHit = false;
}
if(other.gameObject.tag == "Bullet" && health > 0.0f)
{
isShot = false;
}
}
void SetHealthText()
{
healthText.text = "Health: " + health.ToString();
if(health <= 0.0f)
{
dead.text = "YOU ARE DEAD";
}
}
void SetScoreText()
{
scoreText.text = "Score: " + score.ToString();
}
}
Here is my AI script below.
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour {
public float suspition;
public int health = 100;
public Transform target;
public bool foundTarget = false;
public int movespeed;
public int rotateSpeed;
private bool takeDamage = false;
public Transform myTransform;
// Use this for initialization
void Start () {
takeDamage = false;
myTransform = transform;
target = GameObject.FindWithTag("Player").transform;
suspition = 0.0f;
}
// Update is called once per frame
void Update () {
if(takeDamage == true)
{
health -= 10;
}
if(health == 0)
{
gameObject.SetActive(false);
takeDamage = false;
}
if(suspition == 100)
{
foundTarget = true;
}
else{
foundTarget = false;
}
if(foundTarget && health > 0)
{
transform.LookAt(target);
myTransform.Translate(Vector3.forward * movespeed * Time.deltaTime);
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Bullet")
{
takeDamage = true;
}
if(other.gameObject.tag == "Player")
{
foundTarget = true;
}
}
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "Bullet")
{
takeDamage = true;
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "Bullet")
{
takeDamage = false;
}
}
}
Answer by getyour411 · Jan 13, 2014 at 12:04 AM
In the solder script, you set AI via GetCompent which would be the Soldier's AI. As I understand it, you want to access the AI on the Enemy. If that's the case, in your OnTrigger you would do something like
if(other.gameObject.tag == "Enemy")
AI enemyAI = other.gameObject.GetComponent<AI>();
enemyAI.someVar = newValue;
You also might want to determine how you intend to spell Suspicion and be consistent about it.
Follow this Question
Related Questions
What's wrong with my AI script. 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Damage the Player 1 Answer
; expected. Insert a semicolon at the end. 1 Answer
Script error help! 1 Answer