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 /
  • Help Room /
avatar image
0
Question by jinology · Apr 25, 2016 at 10:16 AM · instantiatereferencingarray of gameobjects

I am trying to access transform[] and count from SCRIPT A, however, its not working perfectly. SCRIPT B didnt apply the effect onto the instantiated fish from SCRIPT A. Only some fishes fly to the targets.

SCRIPT A

using UnityEngine; using System.Collections;

public class NemoTarget1 : MonoBehaviour {

 public GameObject targetMesh ;
 public GameObject[] targetspot ;

 public int count;
 public Transform[] targetbody;



 void Start ()
 {
 int count  = targetspot.GetLength(0);
 targetspot = new GameObject [count];
     }

}

////////////////////////////////////////////////////////////////////// SCRIPT B

using System.Collections;

public class projectile3 : MonoBehaviour {

 public Transform[] target = new Transform[6];


 public int n ;


 //    private float firingAngle = 45.0f;
 public float gravity = 900f;
 public Transform Projectile; 
 private Transform myTransform;
 public float Xlimit = -180f;

 void Awake()
 {
     myTransform = transform;  

 }


 void Start()
 {          
     
     //        GameObject targetcount = GameObject.Find ("Nemo_spawner");
     //        NemoTarget nemotarget = targetcount.GetComponent<NemoTarget> ();
     //        int n = nemotarget.count;


     GameObject fishcount = GameObject.Find ("Nemo_spawner"); 
     FishSpawnPoint nemotarget1 = fishcount.GetComponent<FishSpawnPoint> ();
     nemotarget1.count = n; 

// GameObject targetbody = GameObject.Find ("Nemo_spawner"); // FishSpawnPoint nemotarget2 = targetbody.GetComponent (); // nemotarget2.TargetPoint = Target;

     {    
         {
             
         {
             target [n] = GameObject.Find ("Target" + (n + 1)).transform;
         }

     

             StartCoroutine (SimulateProjectile ());

         }
     }
 }


 IEnumerator SimulateProjectile()


 {
     Transform Target; // added codes


     GameObject fishcount = GameObject.Find ("Nemo_spawner"); 
     FishSpawnPoint nemotarget1 = fishcount.GetComponent<FishSpawnPoint> ();
     nemotarget1.count = n; 

     target [n] = GameObject.Find ("Target" + (n + 1)).transform;


     float yRange = 900f; //changing this value will slow down low angle's moving speed
     float yLow = -488f;
     float throughY = (Projectile.position.y - yLow) / yRange;
     float firingAngle = 50.0f - 40.0f * throughY;


     //        GameObject[] targetPoints = GameObject.Find ("Target 1");
     int targetIndex = Random.Range (0, target.Length); // added codes


     Target =  target [targetIndex];  

     yield return new WaitForSeconds(0.2f);


     // Move projectile to the position of throwing object + add some offset if needed.
     Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);

     // Calculate distance to target
     float target_Distance = Vector3.Distance(Projectile.position, Target.position);


     // Calculate the velocity needed to throw the object to the target at specified angle.
     float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);

     // Extract the X  Y componenent of the velocity
     float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
     float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);

     // Calculate flight time.
     float flightDuration = target_Distance / Vx;

     // Rotate projectile to face the target.
     Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);


     float elapse_time = 0;
     while (elapse_time < flightDuration)




     {
         Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
         elapse_time += Time.deltaTime;
         yield return null;


     }


 }   


}

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

0 Replies

· Add your reply
  • Sort: 

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

59 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

Related Questions

Instantiate prefabs at different position 2 Answers

I'm currently trying to make an inventory system and not sure what the best way is to display an items info 0 Answers

instantiate from two seperate values in for loop 1 Answer

How to instantiate an array of prefabs into an array of empty gameobject positions 0 Answers

Any other ways for instantiated prefabs to reference game-objects in the scene? 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