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 /
  • Help Room /
avatar image
0
Question by harpoaian · Nov 29, 2017 at 03:10 PM · c#instantiateprefabsscript reference

Instantiated Prefab won't instantiate with a script reference

Good Morning,

TL;DR: My prefabs aren't instantiating with the playerstatemachine script reference and I cannot drop the playerstatemachine script on the prefab when it is in the prefab folder. How to fix?

I have been slamming my head against the wall on this issue for a couple of days now. Here is the issue, in my prototype I have various things being instantiated from bullets to grenades, and even enemies. I have just now discovered that when my grenades and enemies are instantiated they are missing the reference to my PlayerStateMachine script. With out that script in the slot they grenades and enemies don't function properly. I have looked around the internet for solutions and I found that I have to put the playerstatemachine on the prefab so that the script can be called. I tried that and I get this error KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[PlayerStateMachine+PlayerStates,System.Action].get_Item (PlayerStates key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) PlayerStateMachine.Update () (at Assets/The Guardian/Scripts/PlayerStateMachine.cs:178)

if I put the grenade prefab or the enemy prefabs in the scene I can fill out the slot and it works as intended BUT when the instantiated items are created it doesn't run any of the functions that require the playerstatemachine reference. Here is my Grenade script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 using System;
 
 public class Grenade : MonoBehaviour
 {
 
     [SerializeField]
     private float throwForce;
 
     //public GameObject explosionEffect;
 
     public float delay = 3.0f;
     
     public float countdown;
 
     public bool hasExploded = false;
 
     public bool inRange;
 
     public float radius = 5f;
 
     
 
     public GameObject thePlayer;
     [SerializeField]
     PlayerStateMachine players;
 
 
    
     // Use this for initialization
     void Start ()
     {
         
         countdown = delay;
    
         GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1) * throwForce;
     }
     
     // Update is called once per frame
     void Update ()
     {
         countdown -= Time.deltaTime;
 
         if(countdown <= 0.0f && hasExploded == false)
         {
             Debug.Log("My functions should run");
             Explode();
             hasExploded = true;
             Destroy(gameObject);
         }
     }
 
     public void Explode()
     {
         //Instantiate(explosionEffect, transform.position, transform.rotation);
 
         inRange = Physics2D.OverlapCircle(transform.position, radius, 1 << LayerMask.NameToLayer("Player"));
 
         if (inRange == true && hasExploded == false)
         {
             
            players.GetComponent<PlayerStateMachine>().DamageState();
 
             players.GetComponent<PlayerStateMachine>().playerHealth -= 6;
 
             Debug.Log("I should cause explosion damage");
 
             
             
         }
     }
 
     void OnDrawGizmos()
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere(transform.position, radius);
 
     }
 }

If anyone could help me that would be amazing. Have a good day!

Kindly,

Harpoaian

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 harpoaian · Nov 30, 2017 at 11:46 AM 0
Share

Still having the issue bump so everyone can see! :D

avatar image Vojtasle · Nov 30, 2017 at 01:18 PM 0
Share

I dont really know if I got it right but all prefabs you need to instantiate with the script need to have that script attached as component.

avatar image Darkforge317 Vojtasle · Nov 30, 2017 at 03:08 PM 0
Share

Pretty sure he just wants a variable that needs to contain a reference to the script on another object. Not having the script on the prefab itself.

1 Reply

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

Answer by ShadyProductions · Nov 30, 2017 at 01:24 PM

You make a variable:

 PlayerStateMachine players;

and then you use it like this:

 players.GetComponent<PlayerStateMachine>().DamageState();
 players.GetComponent<PlayerStateMachine>().playerHealth -= 6;

But that's NOT how it works..

You need to use GetComponent on the gameobject that has the playerstatemachine (i'm thinking thePlayer gameobject), and then set it equal to your variable.

 players = thePlayer.GetComponent<PlayerStateMachine>();

and from there on you can use it like so:

 players.DamageState();
 players.playerHealth -= 6;
Comment
Add comment · Show 10 · 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 harpoaian · Nov 30, 2017 at 02:04 PM 0
Share

Alright thank you so far so good! I applied the changes and I am getting places! I am now running into this issue NullReferenceException: Object reference not set to an instance of an object PlayerState$$anonymous$$achine.DamageState () (at Assets/The Guardian/Scripts/PlayerState$$anonymous$$achine.cs:837) Grenade.Explode () (at Assets/The Guardian/Scripts/Grenade.cs:69) Grenade.Update () (at Assets/The Guardian/Scripts/Grenade.cs:54) I created a variable for my player data model script and set it just like I did for my Statemachine and I tried this:

  public void Explode()
     {
         //Instantiate(explosionEffect, transform.position, transform.rotation);
 
         inRange = Physics2D.OverlapCircle(transform.position, radius, 1 << Layer$$anonymous$$ask.NameToLayer("Player"));
 
         if (inRange == true && hasExploded == true)
         {
             data$$anonymous$$odel.ColorTimeIncrease();
            players.DamageState();
 
             players.playerHealth -= 7;
 
             Debug.Log("I should cause explosion damage");
 
             
             
         }
     }

and I am still getting the error that I just showed you. Here is the line of code giving me the issue in my PlayerState$$anonymous$$achine script thePlayer.ColorTimeIncrease(); I would think that making a reference to the ColorTimeIncrease function in my data$$anonymous$$odel would fix this issue. Am I understanding it wrong? I hope I am making sense XD

avatar image sinjimonkey · Nov 30, 2017 at 02:52 PM 0
Share

I don't think you are setting your players variable, as described in the answer (unless you are doing it on Start(), which is what you typically should be doing). Try adding this just before the first place you reference players in the code

players = thePlayer.GetComponent();

Also make sure your 'thePlayer' and 'data$$anonymous$$odel' variables are not null.

What seems to be happening here is the reference you are using (either 'players' or 'data$$anonymous$$odel') is null. You haven't put anything into it, so Unity has no idea what to perform the action on. You can't call a method on a null reference.

avatar image harpoaian · Nov 30, 2017 at 03:00 PM 0
Share

I do initialize the variables in the start method here is my full code:

 public class Grenade : $$anonymous$$onoBehaviour
 {
 
     [SerializeField]
     private float throwForce;
 
     //public GameObject explosionEffect;
 
     public float delay = 3.0f;
     
     public float countdown;
 
     public bool hasExploded = false;
 
     public bool inRange;
 
     public float radius = 5f;
 
     
 
     public GameObject thePlayer;
     //[SerializeField]
     PlayerState$$anonymous$$achine players;
     Player data$$anonymous$$odel;
 
 
    
     // Use this for initialization
     void Start ()
     {
 
         players = thePlayer.GetComponent<PlayerState$$anonymous$$achine>();
         data$$anonymous$$odel = thePlayer.GetComponent<Player>();
         countdown = delay;
    
         GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1) * throwForce;
     }
     
     // Update is called once per frame
     void Update ()
     {
         countdown -= Time.deltaTime;
 
         if(countdown <= 0.0f && hasExploded == false)
         {
             Debug.Log("$$anonymous$$y functions should run");
             hasExploded = true;
             Explode();
             
             Destroy(gameObject);
         }
     }
 
     public void Explode()
     {
         //Instantiate(explosionEffect, transform.position, transform.rotation);
 
         inRange = Physics2D.OverlapCircle(transform.position, radius, 1 << Layer$$anonymous$$ask.NameToLayer("Player"));
 
         if (inRange == true && hasExploded == true)
         {
             data$$anonymous$$odel.ColorTimeIncrease();
            players.DamageState();
 
             players.playerHealth -= 7;
 
             Debug.Log("I should cause explosion damage");
 
             
             
         }
     }

I am confused @sinjimonkey would you be able to provide more clarification? Thank you by the way for helping me out :)

avatar image ShadyProductions harpoaian · Nov 30, 2017 at 03:12 PM 0
Share

Seems like something is null at PlayerState$$anonymous$$achine.cs line 837 Please share the code around this line, preferably not just this line. Error messages aren't always accurate.

avatar image harpoaian ShadyProductions · Nov 30, 2017 at 03:32 PM 0
Share

Here is the function in which the error pops up. thePlayer.ColorTimeIncrease(); is line 837

 public void DamageState()
     {
         thePlayer.ColorTimeIncrease();
         if(hasBeenHit == false)
         {
             playerHealth--;
             thePlayer.TakeDamage();
             hasBeenHit = true;
             gameObject.GetComponent<Renderer>().material.color = Color.red;
         }
         if(playerHealth <= 0)
         {
             Destroy(gameObject);
         }
         else if(thePlayer.ColorTimer () >= thePlayer.ChangeGray())
         {
             gameObject.GetComponent<Renderer>().material.color = Color.gray;
             thePlayer.ResetColorTimer();
             hasBeenHit = false;
             HandleHorizontalInput();
             SetState(PlayerStates.RUN);
         }
     }

And here is the colortimeincrease function from my player data model

 public void ColorTimeIncrease()
     {
         colorChange += Time.deltaTime;
     }




Show more comments

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

429 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 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

Instantiate prefab from folder, not scene 0 Answers

How can I in-script create gameobject from prefab? 2 Answers

Destroy a Prefab from an Array? (C#) 2 Answers

Instantiation of my GameObjects spell (from other script) 0 Answers

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 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