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 zak666 · Sep 16, 2019 at 07:34 AM · scriptingbasics

Random.range Selects the same target? (all turrets choose same target???) (Taget is not selected at random?))

hey gang, just wondering why every turret in my scene with the same gun script targets the same object, when its using random.Range So each turret sound be selecting a random object in array?

for some reason every turret targets the same same "TeamOne" or ""TeamTwo" object and its alwys the first object in the array every time I run the game?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class antiFighterGuns : MonoBehaviour
 {
 
     public GameObject[] TargetTs;
     public GameObject TargetT;
     int index;
     public float GunRange = 1200;
     public float nextFire;
     public float fireRate = 0.5f;
     public GameObject LaserObj;
     float Distance;
 
     public bool AllyGun;
 
    
 
     // Update is called once per frame
     void Update()
     {
         if (TargetT == null)
         {
             if (AllyGun == true)
             {
                 TargetTs = GameObject.FindGameObjectsWithTag("TeamTwo");
                 index = Random.Range(0, TargetTs.Length);
                 TargetT = TargetTs[index];
             }
 
             if (AllyGun == false)
             {
                 TargetTs = GameObject.FindGameObjectsWithTag("TeamOne");
                 index = Random.Range(0, TargetTs.Length);
                 TargetT = TargetTs[index];
             }
         }
 
         if (TargetT != null)
         {
             transform.LookAt(TargetT.transform);
             Distance = Vector3.Distance(TargetT.transform.position, transform.position);
         }
 
         if (Distance > GunRange)
         {
             TargetT = null;
         }
 
 
 
 
 
         if (Time.time > nextFire && TargetT != null)
         {
             nextFire = Time.time + fireRate;
             Instantiate(LaserObj, transform.position, transform.rotation);
         }
 
     }
 
 
 
 
 }
 
Comment
Add comment · Show 1
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 SteenPetersen · Sep 16, 2019 at 09:20 AM 0
Share
 TargetTs = GameObject.FindGameObjectsWithTag("TeamTwo");
 index = Random.Range(0, TargetTs.Length);

This code is correct and doing what you expect it to. So I would guess that somehow the units are not tagged correctly.

Try and put in a - Debug.Log(index); - and see what number it makes. $$anonymous$$y guess would be that it is 0. $$anonymous$$eaning that it only adds the first unit to the list.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sisse008 · Sep 16, 2019 at 12:46 PM

your code works fine. check for human errors.

  index = Random.Range(0, TargetTs.Length);
     Debug.Log("me: " + gameObject.name + "  index = " + index);
 

Comment
Add comment · Show 1 · 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 Bonfire-Boy · Sep 16, 2019 at 02:12 PM 0
Share

I would add TargetTs.Length to that logging (maybe it's only finding one - that'd explain why it always randomises to the same index)

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

119 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

Related Questions

Project Scripting - Tracking variables (General Question) 2 Answers

How Do You Parent an Object With Scripts? 1 Answer

What does the word for mean in javascript? 1 Answer

Why button is not display the window 2 Answers

Game is Paused Menu shows up 0 Answers


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