• 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 JediYinYang · Jan 12, 2016 at 08:33 PM · getcomponent

Getting a variable into my cloned prefab - Null Reference - C#

Newb question.

I have a Torpedo Launcher that Instantiates a Torpedo from a prefab. The Launcher has a script called 'WeaponControl' attached with a public variable called 'Launch Velocity MPS'.

The Torpedo has it's own script that uses AddForce to start chugging along forward. So the Torp works. BUT, I can't for the life of me get the Launch Velocity MPS variable. Thus it starts really slow. I t$$anonymous$$nk I should be using GetComponent to get the Launch Velocity MPS but I can't even get that to work. All it returns it NULL.

Here is the Torp Script:

 using UnityEngine;
 using System.Collections;
 
 public class TorpThrus : MonoBehaviour {
     public int thrust;
     public Rigidbody rb;
     private WeaponControl launchThrust;
 
     void Start () {
         launchThrust = GetComponent<WeaponControl> ();
         Debug.Log (launchThrust);
         rb = GetComponent<Rigidbody> ();
     }
     
     // Update is called once per frame
     void Update () {
         rb.AddForce (transform.forward * thrust);
     }
 }

And for reference here is the WeaponControl script:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponControl : MonoBehaviour {
     public GameObject weapon01;
     public Transform w1launchPoint;
     public float fireRatePerSecond;
     public float launchVelocityMPS;
 
     public GameObject weapon02;
 
     private float weapon01fired;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         weapon01fired = 0;
     }
     
     // Update is called once per frame
     void Update () {
 
         bool down = Input.GetKeyDown(KeyCode.Space);
         //bool held = Input.GetKey(KeyCode.Space);
         //bool up = Input.GetKeyUp(KeyCode.Space);
 
         if (down) {
             GameObject projectile;
             projectile = Instantiate (weapon01, w1launchPoint.position, w1launchPoint.rotation) as GameObject;
             weapon01fired++;
             projectile.name = "projectile_" + weapon01fired;
         }
     }
 }
 

So, how 'should' I solve t$$anonymous$$s issue?

P.S. I'm using Unity 5.3

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

Answer by vintar · Jan 12, 2016 at 09:12 PM

You could do somet$$anonymous$$ng like t$$anonymous$$s :

 GameObject  projectile = Instantiate (weapon01, w1launchPoint.position, w1launchPoint.rotation)as GameObject;
 projectile.name = "projectile_" + weapon01fired;
 projectile.GetComponent<TorpThrus>().thrust = launchVelocityMPS;

Also, change TorpThrus thrust to a float;

Comment
Add comment · Show 2 · 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 JediYinYang · Jan 13, 2016 at 12:12 AM 0
Share

Actually, I was looking for something in the reverse. I want to bring the launchVelocityMPS into the TorpThrust Script since I've also been unable to use AddForce to the projectile with my WeaponControl script. To be clear, what I want is for the torp to be launched at the LaunchVelocityMPS. I don't care which script executes the command. But I want that variable to come from the Laucher, which has the WeaponControl script attached to it.

Also, I don't want to change the LaunchVelocityMPS value from what it's set to in the editor.

Thoughts?

avatar image JediYinYang · Jan 13, 2016 at 01:19 AM 0
Share

Wait, I got it and you sent me in the right direction. Just too much of newb to realize it. I made some changes to the script, but the thing that I had to switch out of the sample you gave me was the .thrust, since I didn't want to alter it. What I did want to alter was the launchThrust.

It'll post the updated scripts below, but thanks for the a help!

The UPDATED Weapon Control Script: using UnityEngine; using System.Collections;

 public class WeaponControl : MonoBehaviour {
     public GameObject weapon01;
     public Transform w1launchPoint;
     public float fireRatePerSecond;
     public int launchForceNewtons;
     private int projectileLaunchForce;
 
     public GameObject weapon02;
 
     private float weapon01fired;
 
     // Use this for initialization
     void Start () {
         weapon01fired = 0;
     }
     
     // Update is called once per frame
     void Update () {
 
         bool down = Input.GetKeyDown(KeyCode.Space);
         //bool held = Input.GetKey(KeyCode.Space);
         //bool up = Input.GetKeyUp(KeyCode.Space);
 
         if (down) {
             GameObject projectile;
             projectile = Instantiate (weapon01, w1launchPoint.position, w1launchPoint.rotation) as GameObject;
             weapon01fired++;
             projectile.name = "projectile_" + weapon01fired;
             Debug.Log ("The launcher's launchNewtons is currently set to: " + launchForceNewtons);
             projectile.GetComponent<TorpThrus> ().launchNewtons = launchForceNewtons;
             projectile.GetComponent<TorpThrus> ().launchedBy = this.name;
         }
     }
 }

The UPDATED TorpThrus Script: using UnityEngine; using System.Collections;

public class TorpThrus : MonoBehaviour { public int thrust; public Rigidbody rb; public string launchedBy; public int launchNewtons;

 void Start () {
     //launchNewtons = GetComponent<WeaponControl> ().launchNewtons;
     Debug.Log ("my name is: " + this.name);
     Debug.Log ("my thrust is: " + thrust);
     Debug.Log ("I was launched by: " + launchedBy);
     Debug.Log ("my Launch Speed is: " + launchNewtons);
     rb = GetComponent<Rigidbody> ();
     rb.AddForce (transform.forward * launchNewtons);
 }
 
 // Update is called once per frame
 void Update () {
     rb.AddForce (transform.forward * thrust);

 }

}

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

36 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

Related Questions

Referencing a function in a C# script 1 Answer

How do declare a nonspecific class to be defined later. 1 Answer

Referencing variable from another script on another object 3 Answers

GetComponent returns null 0 Answers

Get Vector3 From Another Script 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