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 Deadly Firefly · Apr 03, 2013 at 09:25 PM · raycasttransformcollidersvariablesrpc

Why do I get variable not assigned?

This is my bullet raycast script. Why does this script give me a GameObject victim not assigned? I'm trying to send the GameObject that gets hit by a bullet a message to decrease it's health. The GameObject itself is supposed to be the object that the bullet hits.

 public GameObject victim;
 
 void Start()
 {
 
     RaycastHit hit;
     Ray ray = new Ray(transform.position, transform.forward);


     if(Physics.Raycast(ray, out hit, Range))
     }
         transform.parent = hit.transform;
         victim = hit.collider.gameObject;
         networkView.RPC("TellPlayerToApplyDamage", RPCMode.All);
     }
 }
 
 [RPC]
 void TellPlayerToApplyDamage()
 {
     victim.gameObject.SendMessage(DamageMethodName, Damage, SendMessageOptions.DontRequireReceiver);
 }


UnassignedReferenceException: The variable victim has not been assigned.

Comment
Add comment · Show 10
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 ricardo_arango ♦♦ · Apr 03, 2013 at 09:27 PM 0
Share

Check your curly braces {}. There is a closing brace after if(Physics.Raycast(ray, out hit, Range)

avatar image Deadly Firefly · Apr 03, 2013 at 09:30 PM 0
Share

That was my typo while writing the forum post. Sorry :)

avatar image ByteSheep · Apr 03, 2013 at 09:42 PM 0
Share

Have you assigned the victim variable in the inspector view?

avatar image ricardo_arango ♦♦ · Apr 03, 2013 at 09:55 PM 0
Share

The variable victim could be in another script that is also receiving the TellPlayerToApplyDamage RPC, or the TellPlayerToApplyDamage is being called from another script, not from this Start function.

avatar image Deadly Firefly · Apr 04, 2013 at 08:01 AM 0
Share

The Debug.Log("Set victim variable to: "+hit.collider.gameObject.name);

Debug.Log("Set victim variable to: "+victim.name);

after line 13 gives me:

UnnasignedReferenceException: The variable victim has not been assigned.

Set victim variable to: Terrain

Set victim variable to: PistolBullet

When I shoot at a BluePlayerCollider

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Deadly Firefly · Apr 04, 2013 at 04:15 PM

Hey I managed to figure out what I was doing wrong. I should have used

victim = hit.collider;

instead of

victim = hit.collider.gameObject;

Also I changed the SendMessage to SendMessageUpwards.

Thanks to everyone for your help!

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

14 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

Related Questions

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag, and which should I use when? 1 Answer

Accessing Variables from Another Script 1 Answer

Negative Positions Breaks Raycasts 1 Answer

RaycastHit Collider Question 1 Answer

Continuous detection of object using ray cast 2 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