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 TheSpoonKing · Nov 17, 2016 at 08:03 PM · scripting problemcomponentcompile-error

Get Script Component In Parent

I have two scripts. One is attached to a trigger, "DestroyTruck", and the other is attached to the Camera Rig, "PlayerAudio". The truck is a child of the Camera Rig, and is destroyed by the script when it enters the trigger. In the script attached to the trigger, I am trying to access the "PlayerAudio" script attached to the truck's parent Camera Rig, and change a public variable in that script. When I try to get the script component of the parent, I get this error.

"Assets/Resources/DestroyTruck.cs(6,87): error CS1729: The type 'PlayerAudio' does not contain a constructor that takes '1' arguments"

DestroyTruck.cs

 public class DestroyTruck : MonoBehaviour {
     void OnTriggerEnter (Collider other){
         PlayerAudio Intro = new PlayerAudio(GetComponentInParent<PlayerAudio>);
         Intro.WorkClip = 0;
         Destroy (other.gameObject);
         Destroy (this);
     }
 }

PlayerAudio.cs

 public class PlayerAudio : MonoBehaviour {
     AudioSource audio;
     public AudioClip[] ClipsWait, ClipsWork;
     public double CurrX, CurrY, CurrZ;
     public int WaitTime, WaitClip, WorkClip;
 
     void Start () {
         audio = GetComponent<AudioSource>();
         ClipsWait = new AudioClip[]{(AudioClip)Resources.Load("AudioClips/Hey Get Back To Work"),(AudioClip)Resources.Load("AudioClips/Get To Work Recruit"),(AudioClip)Resources.Load("AudioClips/Get To Work"),(AudioClip)Resources.Load("AudioClips/Hey Are You There")};
         ClipsWork = new AudioClip[]{(AudioClip)Resources.Load("AudioClips/Intro")};
         CurrX = this.gameObject.transform.position.x;
         CurrY = this.gameObject.transform.position.y;
         CurrZ = this.gameObject.transform.position.z;
         WaitTime = 0;
         WaitClip = 0;
         WorkClip = -1;
     }
 
     void Update () {
         if (CurrX == this.gameObject.transform.position.x && CurrY == this.gameObject.transform.position.y && CurrZ == this.gameObject.transform.position.z) {
             WaitTime++;
             if (WaitTime >= 1800) {
                 audio.clip = ClipsWait[WaitClip];
                 audio.Play();
                 WaitTime = 0;
                 if (WaitClip < 3) {
                     WaitClip++;
                 } else {
                     WaitClip = 0;
                 }
             }
         } else {
             CurrX = this.gameObject.transform.position.x;
             CurrY = this.gameObject.transform.position.y;
             CurrZ = this.gameObject.transform.position.z;
             WaitTime = 0;
         }
 
         if (WorkClip != -1) {
             audio.clip = ClipsWork[WorkClip];
             audio.Play();
             WorkClip = -1;
         }
     }
 }

Why does the line,

 PlayerAudio Intro = new PlayerAudio(GetComponentInParent<PlayerAudio>);

cause an error, and what do I need to do in order to properly access that script.

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 Sergio7888 · Nov 17, 2016 at 08:15 PM 0
Share

The correct is PlayerAudio Intro = GetComponentInParent<PlayerAudio>();

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Nov 17, 2016 at 08:04 PM

You can't create a MonoBehaviour instance using new. You need to use AddComponent to add an instance of it to a gameObject.

Even if you could, you don't have a constructor defined on your class that accepts the parameter you're passing.

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 TheSpoonKing · Nov 17, 2016 at 09:46 PM 0
Share

The only thing I need to do is just change the WorkClip variable during runtime. Is there any simple way for DestroyTruck to access the public variable on PlayerAudio?

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

Enabling multiple Monobehaviour Components in a game object 1 Answer

enable and disable script on Unity gameobject 0 Answers

i was following the roll a ball tutorial and in the script component it says there is a compile error and it says the reference script on this behavior is missing i dont know what to do 0 Answers

2D Animation does not start 1 Answer

I can't add a very simple Bolt flow to a gameobject 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