- Home /
gameobject.GetComponent().target is not working
I am setting the target for the AI agent at the runtime. I attached the script on the AI Agent with "AI Character Control" script already attached on it.
But my command :
gameobject.GetComponent<AICharacterControl>().target = player .transform;
to set the target showing error:
the name "AICharacterControl" does not exixt in this context
I accessed the player using:
GameObject.FindGameObjectWithTag("Player");
Can someone plz explain me how to set the target at runtime.
Complete code to set target:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetTarget : MonoBehaviour {
private GameObject player;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
gameObject.GetComponent<AICharacterControl> ().target = player.transform;
}
}
Actually editor automatically completes/provides the possible result we can use but it is not showing anything.
Please, provide more context of your code.
Are you sure you have a C# script called exactly AICharacterControl
? Is the AICharacterControl
class defined in a particular namespace? If so:
gameobject.GetComponent<namespace.AICharacterControl>().target
I have just uploaded a game object(AIThirdPersonController) image and a complete code, so plz review it again.
Can you edit the question and provide the AICharacterControl
script? Or tell me if the class looks like this:
namespace XXX
{
public class AICharacterControl : $$anonymous$$onoBehaviour
{
// ...
}
}
Answer by Abhibundela · Oct 18, 2018 at 07:24 PM
Sorry for the previous Answer, It just removed some part of code.
Below is the Image that shows correct way to write that statement, we just need to provide the namespace for complete reference.
@hexagonius Yes, we have add namespace for the complete reference.