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
1
Question by KitsuneWarriorr · Nov 12, 2014 at 01:18 PM · c#scripting problem

How can I fix this UnassignedReferenceException error?

 using UnityEngine;
 using System.Collections;
 
 public class SlowWall : MonoBehaviour {
     public GameObject Player;
     void Example (){
         Player = GameObject.Find("Player");
     }
     public float ShrunkenSlowDownPercentage = 0.6f;
     public float SlowDownPercentage = 0.8f;
     public float EnlargedSlowDownPercentage = 0.9f;
     void OnTriggerEnter(Collider other)
     {
     
         print("Collision detected with trigger object " + other.name);
         PlayerComponent playerComponent = other.gameObject.GetComponent<PlayerComponent> ();
         //checking if collided with player
         if (playerComponent) {
             if (Player.transform.localScale.magnitude < 0.9f){
                 playerComponent.Speed *= ShrunkenSlowDownPercentage;
 
             }
             if (Player.transform.localScale.magnitude > 1.9f){
                 playerComponent.Speed *= EnlargedSlowDownPercentage;
 
             }
             else{
                 playerComponent.Speed *= SlowDownPercentage;
 
             }
             if (playerComponent.Speed < 30f){
                 playerComponent.Speed = 30f;
             }
             Destroy (gameObject);
         }
     }
 
     
     void OnTriggerStay(Collider other)
     {
         print("Still colliding with trigger object " + other.name);
 
     }
     
     void OnTriggerExit(Collider other)
     {
         print(gameObject.name + " and trigger object " + other.name + " are no longer colliding");
     }
 
 }


Okay so here's the error UnassignedReferenceException: The variable Player of SlowWall has not been assigned. You probably need to assign the Player variable of the SlowWall script in the inspector. SlowWall.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Obstacles/SlowWall.cs:19)

Now here's the problem, the slowall script is for a generated object, so how can I get this generated object to atomatically assign my Player to it's script? Any help or advice is welcome and sorry, I'm new to programming.

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
2
Best Answer

Answer by CHPedersen · Nov 12, 2014 at 01:36 PM

The exception is telling you that you need to assign the Player variable to something, which is often done in the inspector by dragging the GameObject Player onto the variable "Player" in this script.

It's also possible to set it in code, and you actually have code in place that does just that: The call to GameObject.Find("Player") which takes place in method Example(). Unfortunately, Example() is never called anywhere, so the variable remains unset.

Set the variable in the inspector and the error goes away.

PS: I took the liberty to edit your question in order to format your code correctly. Unity Answers often does a terrible job highlighting code automatically on pasting, and will frequently miss leading or ending braces, etc. Next time, mark the code manually after pasting and hit the "101010" button.

Comment
Add comment · Show 4 · 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 KitsuneWarriorr · Nov 12, 2014 at 01:41 PM 0
Share

The thing is that the script itself will only accept prefabs which doesn't work for this case. As I said it's a generated object so I need it to find and assign Player itself but I don't know how to

avatar image CHPedersen · Nov 12, 2014 at 01:43 PM 1
Share

Ah, right. Then you just need to make sure the code that sets the Player actually gets called. If I were you, I think I would just rename "Example()" to "Start()". Then Unity will call it automatically when the script is loaded, and set the Player variable for you.

avatar image KitsuneWarriorr · Nov 12, 2014 at 01:47 PM 0
Share

Thanks a bunch, much apreciated

avatar image CHPedersen · Nov 12, 2014 at 01:49 PM 0
Share

$$anonymous$$y pleasure. Thank you for marking the answer correct. :)

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

27 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unable to disable a C# script with a JS Script 2 Answers

Change AnimationCurve keyframe values from script 0 Answers

witch approach for an car race curve 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