• 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 /
  • Help Room /
avatar image
0
Question by Ashton-Benedict · Aug 28, 2016 at 01:49 AM · c#functionaccessing scripts

Cant Access Function in Other Script C#

So I'm having a lot of trouble calling a function from another script. The script that the function is in, is always null (I've tested). I need it not to be null so the function can be called. So, the script that I'm trying to access is attached to a prefab in my assets folder, I'm pretty sure that that's the problem, but I don't know how to fix it. Please help me.

(I cut all of the irrelevant stuff out of the scripts)<<<<<<<<

Script I'm trying to call the function from.

     public class Glock18Firepoint : MonoBehaviour {

     private BulletTrail bulletScript;
 
     void Awake()
     {
         bulletScript = FindObjectOfType<BulletTrail>();
     }
 
 public void Shoot()
     {
         {
             Debug.Log("is bulletScript null? " + (bulletScript == null));
             if (bulletScript)
             {
                 bulletScript.Move();
             }
         }
 }

Script I'm trying to access.

 public class BulletTrail : MonoBehaviour {
 
  public void Move()
     {
         //My code here
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ashton-Benedict · Aug 29, 2016 at 03:16 AM

YES!!! I did it, I re-did my whole system pretty much, I deleted all my code on the script trying to access that one script, then I got rid of the script I was trying to access and put the code from that script onto the script were I had deleted the code/ the one I was trying to call the function from. And I changed the code to make it work..... And deleted another script I was using that was attached to my player that was calling a function in the script that I was trying to call that one function from. Its sooo confusing to explain. as i'm typing this I'm realizing you probably have no idea what I'm trying to say. SO BASICALLY, I had a script attached to my player, I had a script attached to my "FirePoint" (spawner), and another script attached to the bullet prefab itself. And NOW I just have ONE script attached to my spawner/ "FirePoint".

Dust-Cloud and LegionStudios, you both helped realize what the problem was and helped me come to this conclusion thank you so much.

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
1

Answer by Dust-Cloud · Aug 28, 2016 at 01:56 AM

My best guess would be its null because you calling the FindObjectOfType function in the Awake function.

Awake is called right as an object is created so if the Glock18FirePointer is being spawned before the BulletTrail causing the findobject to return null(because the bullet trail isn't spawned at that point). Remember if you have two objects being soawned at the sametime in reality, one has to be created before the other, that is why start is usefull because it waits for all other objects to initialize.

Solution: call FindObjectOfType in Start() function or ensure that there is a bullet trail in existance when the Glock is spawned

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 Ashton-Benedict · Aug 28, 2016 at 04:04 AM 0
Share

putting it into the Start() function didn't work. But how am I supposed to have the bullet trail in existence before firepoint is spawned. The firepoint spawns the bullet trails, and the bullet trails get destroyed after 1 sec.

EDIT: I've been playing around with it, I put it in Update() function and it is calling the function. But it still doesn't work right. When I first shoot it says that the script is null and the bullet spawns and freezes in place (the function makes the bullet move), but when I shoot again it says its not null and it calls the function, but it calls the function for the first bullet that was spawned and another new bullet has been spawned and is now frozen, and repeat. How do I fix that, I know why its happening (the bullet trail doesn't exist the first time I shoot, but I have no idea why its not calling the function for the next bullet I spawn immediately.

avatar image
0

Answer by LegionStudios · Aug 28, 2016 at 05:54 AM

Hey, just to make sure: is the prefab with the tryed-to-be-accesses script has an instance in your scene? because if it doesn't I'm pretty sure you won't be able to retrieve it using "FindObjectOfType". I would try having an instance of that prefab in your scene and then running. Alternatively, you can define a reference to your wanted script in your caller script, and drag it in the inspector.

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 Ashton-Benedict · Aug 28, 2016 at 10:20 PM 0
Share

Ya, I don't have the prefab in my scene, but if I do, then when I shoot my gun, the script is going to be called for that object and it will disappear after 1 sec, so its useless, right? what exactly would you do in this situation? I'm just trying to spawn my bullets correctly and make a working gun....

Ins$$anonymous$$d of using "FindObjectOfType", since my prefab is my assets folder is there a way to find the prefab in my assets folder ins$$anonymous$$d of it trying to look for it in my hierarchy?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

224 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is this bug known? function parameter s and if statements c# 1 Answer

How to call functions from another script c# (Unity5) 1 Answer

Load Scene Unity by Application.LoadLevelAsync 0 Answers

How can I access an int variable from another script? 1 Answer

Moving sine wave curve 2D 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges