• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MorganSkillicorn · Apr 25, 2019 at 12:02 PM · scripting problemscript error

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MorganSkillicorn · Apr 25, 2019 at 12:46 PM 0
Share

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?

avatar image ShadyProductions MorganSkillicorn · Apr 25, 2019 at 12:55 PM 0
Share

change Component type to the actual type so Player$$anonymous$$anager

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

181 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object flashing in and out every frame (because of my script) 0 Answers

Movement Script stops working 0 Answers

"StateMachineBehaviour" Not working for sub sates ? 0 Answers

AudioSource.PlayClipAtPoint creating unlimited AudioOneShot 1 Answer

Need help fixing my script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges