• 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 Xatoku · Feb 16, 2011 at 10:44 PM · projectilethird-person

Advanced Projectile

So currently, my script will allow me to move, and when I press 1, it shoots 1 projectile. The only problem is after it finishes shooting, I get a nullreferenceexception error, stating that there's a problem on line 38. How can I stop this and allow my character to move once he's shot the projectile?

Here's the code:

// Movement Variables: private var jumpSpeed:float = 15.0; private var gravity:float = 50.0; private var runSpeed:float = 35; private var walkSpeed:float = 35; private var rotateSpeed:float = 250.0; private var grounded:boolean = false; private var moveDirection:Vector3 = Vector3.zero; private var isWalking:boolean = true; private var isMoveable:boolean = true; private var isCasting:boolean = false; private var isReadyToShoot:boolean = false; private var moveStatus:String = "idle"; private var xSpeed = 250.0; private var ySpeed = 120.0; private var yMinLimit = -40; private var yMaxLimit = 80; private var x = 0.0; private var y = 0.0; private var dead = false; private var savedTime = 0;

var meteorPrefab:Transform;

// ------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------Animations --------------------------------------- // -------------------------------------------------------------------------------------------------------------

function Do1() { yield WaitForSeconds(1.3); isReadyToShoot = true; }

function Do2() { var meteor = Instantiate(meteorPrefab, GameObject.Find("meteorSpawnPoint").transform.position, transform.rotation); meteor.rigidbody.AddForce(transform.forward * 2000); GameObject.Find("meteorSpawnPoint").active= false; yield WaitForSeconds(0.6); isMoveable = true; isWalking = true; isCasting = false; isReadyToShoot = false; } // ------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------- Death --------------------------------------- // -------------------------------------------------------------------------------------------------------------

function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "fallout") { dead = true; //subtract life here HealthControl.LIVES -= 1; } } // ------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------- Movement --------------------------------------- // -------------------------------------------------------------------------------------------------------------

function Update () { // Only allow movement and jumps while ----------------- GROUNDED ------------- if(grounded) { moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) { moveDirection = .7; } moveDirection = transform.TransformDirection(moveDirection); moveDirection = isWalking ? walkSpeed : runSpeed; moveStatus = "idle"; if(moveDirection != Vector3.zero) { moveStatus = isWalking ? "walking" : "running"; if (isWalking) { animation.CrossFade("run"); } else { // call RUN animation here } } else { animation.CrossFade("Take 001"); } } if(isCasting == true) { animation.CrossFade("meteor"); isMoveable = false; isWalking = false; Do1(); } if(isReadyToShoot == true) { Do2(); } // ------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------- Specials ------------------------------------ // -------------------------------------------------------------------------------------------------------------

// Meteor Special

 if(isCasting == false)
 {
     if(Input.GetKeyDown("1"))
     {
         isCasting = true;
     }
 }

// ------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------- Controls ------------------------------------ // -------------------------------------------------------------------------------------------------------------

 // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down. 
 if(Input.GetMouseButton(1))
 {
     transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0); 
 } else { 
     transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0); 
 } 

 // Toggle walking/running with the T key 
 if(Input.GetKeyDown("t"))
 {
     isWalking = !isWalking; 
 }

 //Apply gravity 
 if(isMoveable)
 {
     moveDirection.y -= gravity * Time.deltaTime; 
 }
 //Move controller 
 if(isMoveable)
 {
     var controller:CharacterController = GetComponent(CharacterController); 
     var flags = controller.Move(moveDirection * Time.deltaTime); 
     grounded = (flags & CollisionFlags.Below) != 0; 
 };
 }
 static function ClampAngle (angle : float, min : float, max : float) { 

if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }

// ------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------- END UPDATE -------------------------------- // -------------------------------------------------------------------------------------------------------------

function LateUpdate() { if(dead) { transform.position = Vector3(1341.832,0.1636853,1158.249); gameObject.Find("Camera") .transform .position = Vector3(1341.832,0.1636853,1135.249); dead = false; } }

// ------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------- END LATEUPDATE -------------------------------- // ------------------------------------------------------------------------------------------------------------- @script RequireComponent(CharacterController)

Comment
Add comment · Show 11
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 Uriel_96 · Feb 17, 2011 at 12:01 AM 0
Share

so let me see your error is in var meteor = Instantiate(meteorPrefab, GameObject.Find("meteorSpawnPoint").transform.position, transform.rotation); rigth??

avatar image Xatoku · Feb 17, 2011 at 12:03 AM 0
Share

I think so, once I do the shoot, it works how I want but I can't shoot again once I've done it once.

avatar image Uriel_96 · Feb 17, 2011 at 12:08 AM 0
Share

do your meterPrefab disappear when you shoot for 2 time(I mean in the object where you have the script)?

avatar image Xatoku · Feb 17, 2011 at 12:10 AM 0
Share

Well basically, the first time works flawlessly. The second time, it plays the animation and at 1.3 (when it should shoot the projectile) it doesn't, and stays on that frame forever.

avatar image Uriel_96 · Feb 17, 2011 at 12:20 AM 0
Share

Ok, I think I find your problem, as you can see you put GameObject.Find("meteorSpawnPoint").active= false;, so maybe when it shoot again the object can find "meteorSpawnPoint" because active = false, so maybe that is, quit that part and check if it works.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Feb 17, 2011 at 01:01 AM

When you wait for .6 seconds, readyToShoot remains true, and Update is getting called (a lot), each time, will spawn a new one. You may want to set that false before the 'wait'

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 Xatoku · Feb 17, 2011 at 01:10 AM 0
Share

Nope, Im still getting the error and I can't shoot consecutively.

Now it says: NullReferenceException Walker+$Do2$6+$.$$anonymous$$oveNext () (at Assets/Scripts/Walker.js:40) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine_Auto(IEnumerator) Walker:Update() (at Assets/Scripts/Walker.js:98)

avatar image
0

Answer by Uriel_96 · Feb 17, 2011 at 03:43 AM

I just got an idea just do something like this:

function Do1() { yield WaitForSeconds(1.3); GameObject.Find("meteorSpawnPoint").active = true; isReadyToShoot = true; }

function Do2() { var meteor = Instantiate(meteorPrefab, GameObject.Find("meteorSpawnPoint").transform.position, transform.rotation); meteor.rigidbody.AddForce(transform.forward * 2000); GameObject.Find("meteorSpawnPoint").active= false; yield WaitForSeconds(0.6); isMoveable = true; isWalking = true; isCasting = false; isReadyToShoot = false; }

as you can see i add GameObject.Find("meteorSpawnPoint").active= true; so like that it make it true then false and do the same.

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 Xatoku · Feb 17, 2011 at 05:25 AM 0
Share

Still won't let me do it twice. Error is saying:

NullReferenceException Walker+$Do1$3+$.$$anonymous$$oveNext () (at Assets/Scripts/Walker.js:32)

avatar image
0

Answer by TheDemiurge · Feb 17, 2011 at 10:48 AM

As was stated above, GameObject.Find() only returns active objects (it's stated in the docs, too). So the statement GameObject.Find("meteorSpawnPoint").active = true; is meaningless and will most likely also generate an NRE if it gets executed unless you have more objects with that name that are active.

I'm not quite sure what you're even trying to do here, but presumably you're just trying to hide the 'launcher' until the player fires again. If that's true, what you should do is keep a variable of the spawn point so you can either activate/deactivate it, or (the preferred choice) disable its renderer (or meshrenderer as the case may be). Throw something like this into your Awake or Start method:
var meteorSpawnPoint: GameObject = GamEObject.Find("meteorSpawnPoint");
Then use
meteorSpawnPoint.active = true; // or false
OR
meteorSpawnPoint.GetComponent(MeshRenderer).enabled = true; // or false

Edit for comment clarify:

Typically your launch function would look roughly like this:

function DoFireball()
{
if (!isCasting)
{
isCasting= true;
animation.Play("fireball");
yield WaitForSeconds(animation["fireball"].length);
Instantiate (....); // do the launching thing
yield WaitForSeconds(shotDelay); // how often can we fire?
isCasting = false;
}
}

and then your Update function would look like:

function Update()
{
if (Input.GetKeyDown("1"))
{
DoFireball();
}
}
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 Xatoku · Feb 17, 2011 at 12:44 PM 0
Share

Didn't work. Ill try to explain the problem as best as I can.

If I press 1, the character goes to the "meteor" animation, which is him casting a fireball. Because he charges up the fireball, and then shoots on frame 40, I have the yield script in Do1(). For some reason, if I don't put anything under the projectile shooting script to stop the spawn point from spawning, he'll shoot like 200 fireballs and spray them like crazy. But if I do stop the spawn, it will work once, but then If I shoot it again he'll just stop on the last frame of the meteor animation and shoot nothing.

avatar image TheDemiurge · Feb 17, 2011 at 10:12 PM 0
Share

It sounds like the first problem may be caused by using Input.Get$$anonymous$$ey() ins$$anonymous$$d of Input.Get$$anonymous$$eyDown(). The difference is in being true WHILE the key is pressed or WHEN the key is pressed.

I'm a bit confused on why there's a separate object involved here for playing an animation, but if it's playing once and staying at the end it sounds like your animation's wrap$$anonymous$$ode is set to ClampForever. You'll see from the docs that Once is the mode you want.

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

1 Person is following this question.

avatar image

Related Questions

Aiming in the direction of cursor in third person. 2 Answers

Projectile help (shuriken, throwing knife, etc) 1 Answer

Calculate parabolic initial velocity and angle? 0 Answers

Position wall of arrows in front of player 0 Answers

Cannon shoots in wrong direction with unrealisic Physic 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