Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 cyth1 · Jul 06, 2015 at 08:49 PM · instantiatetransform

I'm having trouble understanding what "does not contain a definition for" means.

So inside of my player controller I've have a way to take my current gun and replace it with a new gun (via buy or drop or whatever, thats for later).

 void EquipGun(int i){
         if (currentGun) {
             Destroy(currentGun.gameObject);
         }
         currentGun = Instantiate (guns [i], handHold.position, handHold.rotation) as Gun;
         currentGun.transform.parent = handHold;

 

handHold is my variable for where every gun will be on characters, but instantiating this line with my Gun class is where I get the error because I added the position and rotation for handHold. Here is my Gun class:

 using UnityEngine;
 using System.Collections;
 [RequireComponent (typeof (AudioSource))]
 public class Gun : MonoBehaviour {
 
     public enum GunType {Semi,Burst,Auto};
 
     public LayerMask collisionMask;
     public float gunID;
     public GunType gunType;
     public float rpm;
     public float damage = 1;
 
     public Transform spawn;
     public Transform shellEjectionPoint;
     public Rigidbody shell;
     private LineRenderer tracer;
 
     //System:
     private float secondsBetweenShots;
     private float nextPossibleShootTime;
 
     void Start(){
         secondsBetweenShots = 60 / rpm;
         if (GetComponent<LineRenderer>()) {
             tracer = GetComponent<LineRenderer>();
             }
     }
     
     public void Shoot(){
         if (CanShoot ()) {
 
         Ray ray = new Ray (spawn.position, spawn.forward);
         RaycastHit hit;
 
         float shotDistance = 20;
 
         if (Physics.Raycast (ray,out hit, shotDistance, collisionMask)){
         shotDistance = hit.distance;
 
                 if (hit.collider.GetComponent<Enitity>()){
                     hit.collider.GetComponent<Enitity>().TakeDamage(damage);
 
                 }
     }
 
         nextPossibleShootTime = Time.time + secondsBetweenShots;
         GetComponent<AudioSource>().Play();
 
             if (tracer) {
                 StartCoroutine ("RenderTracer", ray.direction * shotDistance);
             }
 
             Rigidbody newShell = Instantiate(shell, shellEjectionPoint.position,Quaternion.identity) as Rigidbody;
             newShell.AddForce (shellEjectionPoint.forward * Random.Range (150f,200f) + spawn.forward * Random.Range(-10f,10f));
     }
 }
 
     public void ShootContinuous(){
         if (gunType == GunType.Auto) {
             Shoot();
         }
     }
 private bool CanShoot(){
     bool canShoot = true;
     if (Time.time < nextPossibleShootTime){
         canShoot = false;
     }
 
     return canShoot;
 }
     IEnumerator RenderTracer(Vector3 hitPoint){
         tracer.enabled = true;
         tracer.SetPosition (0,spawn.position);
         tracer.SetPosition (1,spawn.position + hitPoint);
 
         yield return null;
         tracer.enabled = false;
     }
 }




Okay sorry, here are my errors, there's six of them.

1 Assets/Scripts/PlayerController.cs(64,62): error CS1061: Type Gun' does not contain a definition for position' and no extension method position' of type Gun' could be found (are you missing a using directive or an assembly reference?)

2 Assets/Scripts/PlayerController.cs(64,81): error CS1061: Type Gun' does not contain a definition for rotation' and no extension method rotation' of type Gun' could be found (are you missing a using directive or an assembly reference?)

3 Assets/Scripts/PlayerController.cs(64,30): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments 4 Assets/Scripts/PlayerController.cs(64,30): error CS1503: Argument #2' cannot convert object' expression to type UnityEngine.Vector3'

5 Assets/Scripts/PlayerController.cs(65,38): error CS0029: Cannot implicitly convert type Gun' to UnityEngine.Transform'

6 Assets/Scripts/PlayerController.cs(66,26): error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'

Thank you, but my gun does have a transform, I have an object "weapon" it's the only thing the Gun script is attached to see: alt text

And thank you for the fix for 4, I put in a quick fix for errors 1, 2, and 3 while I try to figure them out, I still have 1 error left (or rather still 4), error number 6 that comes from my last line with the Animator, I think I can solve that one but I'm trying to understand why Gun have no transform when the object it's attached to does have one.

capture.jpg (35.6 kB)
Comment
Add comment · Show 3
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 Adriansurf · Jul 06, 2015 at 08:58 PM 0
Share

Can you provide full error message?

avatar image hexagonius · Jul 06, 2015 at 09:03 PM 0
Share

happens to me when I forget to save the script and unity only refreshed a referencing one.

But yeah, the full error and the error line would be nice

avatar image cyth1 · Jul 07, 2015 at 06:22 AM 0
Share

There's six error codes

1.Assets/Scripts/PlayerController.cs(64,81): error CS1061: Type Gun' does not contain a definition for rotation' and no extension method rotation' of type Gun' could be found (are you missing a using directive or an assembly reference?)

2.Assets/Scripts/PlayerController.cs(64,62): error CS1061: Type Gun' does not contain a definition for position' and no extension method position' of type Gun' could be found (are you missing a using directive or an assembly reference?)

3.Assets/Scripts/PlayerController.cs(64,30): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments 4.Assets/Scripts/PlayerController.cs(64,30): error CS1503: Argument #2' cannot convert object' expression to type UnityEngine.Vector3'

5.Assets/Scripts/PlayerController.cs(65,38): error CS0029: Cannot implicitly convert type Gun' to UnityEngine.Transform'

6.Assets/Scripts/PlayerController.cs(66,26): error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Adriansurf · Jul 07, 2015 at 06:36 AM

The problem is in PlayerController.cs file. Without it it's hard to fix all errors.

Class Gun is type of MonoBehaviour not Transform so it don't have position and rotation fields out of the box. You have to get this values through transform: change handHold.position to handHold.transform.position. This should fix issue 1, 2, 3.

currentGun.transform.parent = handHold; change to currentGun.transform.parent = handHold.transform; - this will fix 4

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 cyth1 · Jul 07, 2015 at 04:35 PM 0
Share

Okay, I solved problems 1-3. I accidentally made public Gun handHold; ins$$anonymous$$d of public Transform handHold. lol

avatar image
0

Answer by Eric5h5 · Jul 06, 2015 at 11:48 PM

Just what it says: your Gun class has no definition for "position". You have defined "collisionMask", "gunID", "gunType", and so on, but there's no definition for "position". It sounds like you're trying to access the Transform class, which does have definitions for position and rotation. You can't convert from Transform to Gun.

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 sourav13 · Jul 07, 2015 at 07:27 AM

Yes you got to take game object gun I mean on which your gun script is there and than take its 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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Object spawning with transforms 2D 1 Answer

Object instantiate, Projectile problems 2 Answers

MissingMethodException: System.Collections.Generic.List 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

How to instantiate prefab over the text one letter at a time? OR how to get transform of letters in a text 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