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 22, 2016 at 09:01 AM · transformarray of gameobjects

Problem with random tranform using array

Problem is in this line:

 Target = target [targetIndex];

The compiler complains with:

 Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Transfrom'

This is the script:

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 public class projectile2 : MonoBehaviour {
 
     GameObject[] target;
 
 
     //    private float firingAngle = 45.0f;
     public float gravity = 900f;
     public Transform Projectile; 
     private Transform myTransform;
     public float Xlimit = -180f;
     private GameObject target1;
     void Awake()
     {
         myTransform = transform;  
 
     }
 
 
     void Start()
 
 
     {          
 //        target = GameObject.FindGameObjectsWithTag ("point");
 
         StartCoroutine(SimulateProjectile());
 
     }
 
 
     IEnumerator SimulateProjectile()
     {
 Transform Target;
 
 target = new Transform[6];
         target [1] = GameObject.Find ("Target 1").transform.position;
         target [2] = GameObject.Find ("Target 2").transform.position;
         target [3] = GameObject.Find ("Target 3").transform.position;
         target [4] = GameObject.Find ("Target 4").transform.position;
         target [5] = GameObject.Find ("Target 5").transform.position;
         target [6] = GameObject.Find ("Target 6").transform.position;
 
 int targetIndex = Random.Range (0, target.Length);
 
 Target = target [targetIndex];





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 Bunny83 · Apr 22, 2016 at 09:19 AM 0
Share

I took the liberty of cutting down your title and format your code in a way it can be read. I also moved all the information from the title into the question. Please for future questions take some time (at least 1-2 $$anonymous$$utes) to present your problem and don't just copy the error message. If you don't mark code as code it isn't presented right on this website.

I suggest you read the user guide if you want further help here on UnityAnswers.

2 Replies

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

Answer by Bunny83 · Apr 22, 2016 at 09:33 AM

First of all you declared your target array as:

 GameObject[] target;

So it's an array of gameobjects. Inside your SimulateProjectile() method you try to assign a Transform array to the variable. That's not possible an array of apples can't be assigned to an array of oranges.

Next thing is all those lines:

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

don't make much sense. You try to assign the position of that object (which is a Vector3 value) to an element of your target array. No matter if your target array is of type GameObject[] or Transform[] this won't work.

Next thing is your array seems to be ment to hold 6 items since you create it like this: new Transform[6];. However arrays start counting at "0". So the highest index for an array with 6 elements is "5". So you have the indices: (0, 1, 2, 3, 4, 5) which are 6 indices.

Some minor problems here are:

  • Since your "target" variable is an array it actually represents multiple objects. You should give it a plural name like targets.

  • Your script / code doesn't seem to be complete. Keep in mind that your Target variable is declared inside the "SimulateProjectile" method, so it can only be used inside that method.

Things you might want to change:

  • GameObject arrays are quite useless. It's better to use a Transform array. So i would suggest you change your GameObject[] target; to Transform[] targets;

  • When you assign your different targets you should assign their transform objects to the array element and not the position of the transforms and start counting at "0" : targets[0] = GameObject.Find ("Target 1").transform;

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 fishbrainz · Apr 22, 2016 at 09:07 AM

Target = target [targetIndex].transform;

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to set game object transforms 2 Answers

How to access Script B's Transform[] from my current script A? i going to drag scene object's transform into the new array script B. 0 Answers

Moving spawned enemies randomly towards several pre-defined positions 1 Answer

Unity - Opposite of instantiate - destroyed but not removed 0 Answers

Object reference not set to an instance of an object + array of positions 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