Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Vidar · Nov 22, 2010 at 02:10 PM · nullreferenceexceptioninstantiation

Instantiate 2 different Buildings in RTS doesn't work // Null Reference Exception

Hey Guys,

maybe its a real easy problem, but i'm a very beginner with Unity :)

I'm building a RTS Game, and actually i have a little map, and a GUI with 2 Icons for 2 seperate buildings. Now when i click one of the GUIs it instatiates the building and yeah, i can build it. But when i click on the other GUI afterwards, it instaites the other building but moves the first instatiation with the actual one. So i'm moving the new building and the old one i just built. Error is: NullReferenceException: Object reference not set to an instance of an object.

The Code of both GUIs:

var building : Transform; var gridSpacing = 2; var instant; var ray; var hit:RaycastHit;

function OnMouseDown() {

 if(buildingCounts.at_Buildt == true)
 {
     /* var p : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Input.mousePosition.y, 30));
     Instantiation = Instantiate(building, p, Quaternion.identity);
     */
     ray = Camera.main.ScreenPointToRay (Input.mousePosition);

     if (Physics.Raycast (ray, hit, 100)) 
     {
         Debug.DrawLine (ray.origin, hit.point);
         instant = "";
         instant = Instantiate(building, Vector3(hit.point.x, 2.5, hit.point.z),Quaternion.identity);
         instant.name = "Building2_"+(buildingCounts.BUILDING2+1);

     }

     buildingCounts.at_Buildt = false;
 }

}

function Update() {

 if(buildingCounts.at_Buildt == false)
 {
     //var p : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y - 30, 30));
     //Instantiation.transform.position = p;

     ray = Camera.main.ScreenPointToRay (Input.mousePosition);

     if (Physics.Raycast (ray, hit, 100)) 
     {
         Debug.DrawLine (ray.origin, hit.point);
         instant.position.x = hit.point.x;
         instant.position.z = hit.point.z;
         instant.position.y = 2.5;

         instant.position.x = Mathf.Round(instant.position.x / gridSpacing) * gridSpacing;
         instant.position.z = Mathf.Round(instant.position.z / gridSpacing) * gridSpacing;

     }
 }

}

Would be thankful for every help!

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

Answer by Vidar · Nov 22, 2010 at 07:49 PM

ok problem solved, stupid mistake

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 Caiuse · Mar 11, 2011 at 05:24 PM 0
Share

At least explain the problem, and how its was solved before accepting it as an answer.

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

No one has followed this question yet.

Related Questions

iTween can't access instantiated child. 1 Answer

Using iTween with a prefab tree. 0 Answers

Can't reference gameObject in NetworkBehaviour.Awake() 2 Answers

Instantiated prefab references point to first prefab instantiated 3 Answers

Why Is the game object not selected? I'd like to understand why. 2 Answers


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