Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 XxCAFxX · Dec 30, 2018 at 01:44 AM · scripting problemmultiple objects

Script stops working when on multiple objects

Hopefully i can get some help with this, its been driving me nuts for a couple of days. I'm working on an archery game, my bow and arrow scripts are almost perfect but my ai script is all messed up. I'm sure its just something stupid i set up wrong but anyways, the issue is, i have my script setup to where if an arrow hits certain parts of the ragdoll character it will execute different actions, for example if you get a headshot the character will instantly ragdoll and die. Another example, if you shoot the legs it will cripple the ai to a crawl and play the crawling animation. The problem happens when i spawn in two ai at once, the script works fine if i have only one alive ai in the scene. if i have more than one ai in the scene some of the ai are unresponsive to the arrow when it collides with them and they just keep walking towards the next path node. Heres my code.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class aiController : MonoBehaviour { float rotSpeed = 5f; private float speed = 2; private Transform target; private int wavepointIndex=0; public bool canMove = true; public static aiController instance; private bool canScare = true; private bool walking = false; private bool armBroken = false; private bool groinHurt = false; private bool isDead = false;

 private GameObject ai;

 private Animator aiAnim;

 public AudioSource hurt_brathing;
 public AudioSource lol;

 void Start()
 {
     ai = GetComponent<GameObject>();
     instance = this;
     target = pathNodesController.pathNodes[0];
     aiAnim = GetComponent<Animator>();
 }

 void Update()
 {
     if(isDead==true)
     {
         lol.Stop();
         hurt_brathing.Stop();
     }
     if(speed >= 1.90)
     {
         aiAnim.SetBool("walking", true);
     }
     if (canMove == true)
     {
         Vector3 dir = target.position - transform.position;
         transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
         var targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotSpeed * Time.deltaTime);
         if (Vector3.Distance(transform.position, target.position) <= 0.05f)
         {
             aiAnim.SetBool("walking", false);
             GetNextPathNode();
         }
     }
 }

 void GetNextPathNode()
 {
     if(wavepointIndex >= pathNodesController.pathNodes.Length-1)
     {
         Debug.Log("last Path Node");
         return;
     }

     wavepointIndex++;
     target = pathNodesController.pathNodes[wavepointIndex];
 }

 void OnTriggerEnter(Collider other)
 {
     if(other.tag ==("Scare") && canScare == true)
         {
         aiAnim.SetBool("sprinting", true);
         speed = 4f;
         }
 }

 public void breakLegs()
 {
     aiAnim.SetBool("sprinting", false);
     canScare = false;
     aiAnim.SetBool("crawl", true);
     speed = 0.5f;
     if (hurt_brathing.isPlaying == false&&isDead==false)
     {
         hurt_brathing.Play();
     }
 }

 public void ragDoll()
 {
     isDead = true;
     aiAnim.enabled = false;
     Rigidbody[] bodies = GetComponentsInChildren<Rigidbody>();
     for (int i = 0; i < bodies.Length; i++)
     {
         bodies[i].isKinematic = false;
     }
     StartCoroutine(destroyAI());
 }

 IEnumerator destroyAI()
 {
     yield return new WaitForSeconds(50000 * Time.deltaTime);
     Destroy(gameObject);
 }

 public void BreakLeftArm()
 {
     if (armBroken == false)
     {
         aiAnim.SetBool("leftArm", true);
         speed = 1;
         if (hurt_brathing.isPlaying == false && isDead == false)
         {
             hurt_brathing.Play();
         }
     }
     canScare = false;
     armBroken = true;
 }

 public void BreakRightArm()
 {
     if (armBroken == false)
     {
         aiAnim.SetBool("RightArm", true);
         speed = 1;
         if(hurt_brathing.isPlaying==false && isDead == false)
         {
             hurt_brathing.Play();
         }
     }
     canScare = false;
     armBroken = true;
 }
 public void HitGroin()
 {
     if(groinHurt==false)
     {
         lol.Play();
     }
 }

}

heres an example from my arrow script, howw i execute the code in the ai controller.

    if (collision.collider.tag == "Head")
  {
 aiController.instance.ragDoll();
 aiController.instance.canMove = false;
 transform.SetParent(collision.collider.transform);
   Destroy(rbarrow);
   if (hasHit == false)
  {
           Instantiate(bloodFX[Random.Range(0, bloodFX.Length)], bloodSpwaner.position, bloodSpwaner.rotation);
             Instantiate(bloodFxSkin, bloodSkinSpawner.position, bloodSkinSpawner.rotation, transform.parent);
             Instantiate(bloodSprayFx, bloodSkinSpawner.position, bloodSkinSpawner.rotation, transform.parent);
  }
  hasHit = true;

}

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
0
Best Answer

Answer by XxCAFxX · Dec 30, 2018 at 06:18 AM

Thank you to @Razor1994 for confirming that it was because i was using an instance to call my functions. I managed to figure it out myself. if anyone was having this issue this was my solution, instead of calling aiController.instance.ragDoll(); i instead called:

collision.collider.gameObject.GetComponentInParent().ragDoll();

The script now works as it should (:

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 Razor1994 · Dec 30, 2018 at 03:08 AM

I would say the last ai that has been spawned will always work. And thats because you have a static reference to yourself (singleton pattern). With every new aiController instance you override that instance.

 public static aiController instance;
 
 instance = this;

so all your other aiController instances will now reference the latest aiController instance.

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 XxCAFxX · Dec 30, 2018 at 05:26 AM 0
Share

Thanks for the response, i figured it was something to do with the way im accessing it through the other script, but how would i go about fixing this while retaining the ability to access the ai controller from my arrow controller?

avatar image Sabre-Runner XxCAFxX · Dec 30, 2018 at 07:39 AM 0
Share

I believe you need to either duplicate the controller and forgo the singleton or abstract the controller into a system that operates on the individual characters.

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.

Follow this Question

Answers Answers and Comments

171 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

Related Questions

Independently using the same script on multiple game objects? 2 Answers

OnTriggerEnter Script only working on duplicated object 1 Answer

non-static Variable in script on multiple gameObjects reset. Why? 1 Answer

I want to move two bar simultaneously.How can I do?,I want to move multiple object with using tags,so How can I do ? 0 Answers

Multi-gun Aiming System 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