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 /
This question was closed Jun 13, 2016 at 09:59 AM by Boatkilled for the following reason:

It was my bad

avatar image
0
Question by Boatkilled · Jun 14, 2016 at 06:14 PM · prefabnullreferenceexceptionbulletprefab-instanceinstatiate

Problem when setting variable in an instantiated gameobject

The premise is this: I wanted to find a way to control the behavior of an instantiated bullet without having to make a different prefab every time,so I made a base Bullet class from which new classes can be created to override the Start() and Update() functions in the base class and then attach the new class to a GameObject. Here's the relevant code. Base bullet class:

 public class Bullet
 {
     public Weapon weapon;
     public Sprite bulletSprite;
     public Transform bulletBody;
     public float speed;
 
     public virtual void Start()
     {
         bulletBody.GetChild(0).GetComponent<SpriteRenderer>().sprite = bulletSprite;
     }
 
     public virtual void Update()
     { }
 }

The class I'm trying to get to work:

 public class NormalBullet : Bullet
 {
     public override void Start()
     {
         bulletSprite = Resources.Load<Sprite>("Bullets\bullet01");
         base.Start();
     }
 
     public override void Update()
     {
         bulletBody.Translate(bulletBody.forward * weapon.bulletSpeed);
     }
 }

The script on the actual bullet prefab

 public class BulletScript : MonoBehaviour {
 
     public Bullet bullet;     //This returns null after it is instantiated 
 
     void Start () 
     {
         bullet.Start();
     }
     
     void Update () 
     {
         bullet.Update();
     }
 }

and Finally were the bullet is instantiated (I pass a Bullet class previously created which is "bulletScript" to the prefab):

 public override void Fire()
     {
         GameObject firedBullet = GameObject.Instantiate(bulletPrefab,gunTransform.position,gunTransform.rotation) as GameObject;
         Bullet firedBscript = firedBullet.GetComponent<BulletScript>().bullet;
 
         firedBscript = bulletScript;
         firedBscript.bulletBody = firedBullet.transform;
         firedBscript.weapon = this;

         //If I check whether firedBullet has a "bullet" class set it says everything is as it should be
     }

When I try to fire the bullet it throws a nullReferenceException from the prefab I'm firing saying there's no Bullet class attached. I checked with Debug.Log and apparently the variable IS set the moment I instantiate the bullet but for some reason the Bullet variable on the bullet itself returns null. inb4 I'm actually blind and it was something really simple to fix.

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 Boatkilled · Jun 13, 2016 at 09:58 AM 0
Share

GG friends I'm actually blind,the problem was that I mistakenly attached a BulletScript to a bullet prefab child and that was the source of the error messages.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accessing a prefab after instantiating results as null 1 Answer

Prefabs spawn with wrong values 0 Answers

Possible to make "override all" button on prefab instances ignore certain values (like it does for position)? 0 Answers

Nullreference? 1 Answer

Using Resources.load with a variable 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