Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jadaith · Sep 27, 2013 at 01:12 AM · c#c #

Question about GetComponent

I'm running into a error with the GetComponent portion of my script. Can anybody tell me what I missed? I have the script its looking for attached to the gameobject. But I'm still getting this error in the console

" Type UnityEngine.Object' does not contain a definition for GetComponent' and no extension method GetComponent' of type UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)"

 using UnityEngine;
 using System.Collections;
 
 public class AirTurret : MonoBehaviour {
     
     public GameObject myProjectile;
     public float reloadTime = 1f;
     public float turnSpeed = 5f;
     public float firePauseTime =.25f;
     public float errorAmount = .001f;
     public Transform myTarget;
     public Transform[] muzzlePositions;
     public Transform pivot_Tilt;
     public Transform pivot_Pan;
     public Transform aim_Pan;
     public Transform aim_Tilt;
     
     private float nextFireTime;
 
     // Use this for initialization
     void Start () {
     }
     
     void Update () {
         if(myTarget)
         {
             aim_Pan.LookAt(myTarget);
             aim_Pan.eulerAngles = new Vector3(0, aim_Pan.eulerAngles.y, 0);
             aim_Tilt.LookAt(myTarget);
             
             pivot_Pan.rotation = Quaternion.Lerp(pivot_Pan.rotation, aim_Pan.rotation, Time.deltaTime * turnSpeed);
             pivot_Tilt.rotation = Quaternion.Lerp(pivot_Tilt.rotation, aim_Tilt.rotation, Time.deltaTime * turnSpeed);
             
             if(Time.time >= nextFireTime)
             {
                 FireProjectile();
             }
         }
     }
     
     void OnTriggerEnter(Collider other )
     {
         if(other.gameObject.CompareTag("Enemy"))
         {
             nextFireTime = Time.time +(reloadTime*.5f);
             myTarget = other.gameObject.transform;
         }
     }
     
     void OnTriggerExit(Collider other)
     {
         if(other.gameObject.transform == myTarget)
         {
             myTarget = null;
         }
     }
     
     void FireProjectile()
     {
 //        audio.Play();
         nextFireTime = Time.time+reloadTime;
         
         int m = Random.Range(0,6);
         var newMissile = Instantiate(myProjectile, muzzlePositions[m].position, muzzlePositions[m].rotation);
         newMissile.GetComponent<Projectile_Missle>().myTarget = myTarget;
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Sep 27, 2013 at 01:23 AM

Replace line 64 with this:

    GameObject newMissile = Instantiate(myProjectile, muzzlePositions[m].position, muzzlePositions[m].rotation) as GameObject;

By using 'var' and by not casting the Instantiate() somehow, the compiler sets newMissile to a base type (and not a game object).

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 Eric5h5 · Sep 27, 2013 at 01:44 AM 0
Share

The problem isn't the use of "var", it's that it was missing the "as GameObject" cast. The cast will cause the type to be correct if you use var.

avatar image jadaith · Sep 27, 2013 at 01:53 AM 0
Share

Thanks for your help guys. I had to look up what exactly you meant by cast and I got it from there. Thanks for the information.

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to reference a variable from a different script? 1 Answer

How to make tab in editor near the top? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Planetary gravity for not so round object? 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