- Home /
get my scripts to talk to each other
I am struggling to access a custom script from the standard asset AICharacterController, the script is attached to an empty game object that I am using as a game manager. I have edited the AI script to access my manager object but I am unable to get the component script that I have made. Could this be a namespace issue? anyway here is my code : ) thanks
using System;
using UnityEngine;
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof(UnityEngine.AI.NavMeshAgent))]
[RequireComponent(typeof(ThirdPersonCharacter))]
public class AICharacterControl : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent agent { get; private set; }
public ThirdPersonCharacter character { get; private set; }
public Transform target;
Animator animator;
public GameObject gameManager;
public GameObject player;
Component managerScript;
private void Start()
{
agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
animator = GetComponent<Animator>();
gameManager = GameObject.Find("GameController");
managerScript = GetComponent<PlayerManager>(); //This should find the script attatched to the game object
player = GameObject.Find("Player");
agent.updateRotation = false;
agent.updatePosition = true;
SetTarget(player.transform);
}
private void Update()
{
if (animator.GetBool("Dead"))
SetTarget(agent.transform);
managerScript.TakeDamage(1); //this should edit the health
if (target != null)
agent.SetDestination(target.position);
if (agent.remainingDistance > agent.stoppingDistance)
character.Move(agent.desiredVelocity, false, false);
else
character.Move(Vector3.zero, false, false);
}
public void SetTarget(Transform target)
{
this.target = target;
}
}
}
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
public GameObject player;
public float maxHealth = 100;
float currentHealth;
bool alive = true;
bool dead = false;
public Camera cam1;
public Camera cam2;
// Start is called before the first frame update
void Start()
{
cam1.enabled = true;
cam2.enabled = false;
currentHealth = maxHealth;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("9"))
currentHealth -= 100;
if (currentHealth <= 0)
{
alive = false;
}
if (!alive && !dead)
{
Die();
}
}
public void TakeDamage(float ammount)
{
currentHealth -= ammount;
}
private void Die()
{
SwitchCam();
player.transform.position = new Vector3(170, 10, -220);
dead = true;
}
private void SwitchCam()
{
cam1.enabled = !cam1.enabled;
cam2.enabled = !cam2.enabled;
}
public bool IsDead()
{
return !alive;
}
}
I have commented the lines that have issues
Answer by MorganSkillicorn · Apr 25, 2019 at 01:01 PM
So, it turns out the script was not being seen by the standard asset script as it was not in the same folder. I have moved it and it works fine now.
thanks @Vollmondum & @ShadyProductions
Answer by Vollmondum · Apr 25, 2019 at 12:34 PM
Remove you Component line as a variable and address component each time it's needed directly. Your "should" comment answers it. It should not :) Or it never did, or doesnt want to
I have changed it to this
game$$anonymous$$anager.GetComponent().TakeDamage(1);
but it still has no reference of the component Player$$anonymous$$anager how would I access this?
change Component type to the actual type so Player$$anonymous$$anager
Your answer
![](https://koobas.hobune.stream/wayback/20220612200207im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Draw call minimizer 1 Answer
Help with script 1 Answer
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer
Respawn Point dont work, is because is a prefab? 2 Answers
How to share varibles betweent scripts 4 Answers