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 /
This question was closed Sep 23, 2013 at 12:21 PM by Ranger-Ori for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ranger-Ori · Feb 04, 2013 at 09:51 AM · physicsdesignpattern

Can't create a Factory (Design Pattern)

Hello everyone,

I was here in this forum a while ago, and I'm learning Software Engineering at the moment. I'm using Unity3d in my spare time.

I created a game long before I started my degree, and while I'm working on it, I've learnt a lot of new things in coding, especially Design Pattern called "Business Layers" pattern.

Now I want to convert all my scripts that were in Javascript to C#.

I've created a Jet that flies, and I want to implement him my physics, through my C# solution, and not through JS. I'm creating a new "Unity C#" file in order to convert everything.

And here is the error I encountered, there is no namespace in the C# unity file, and somehow I can't create a new "FactoryPhysics" object or even use Inheritance.

Then how to solve this issue? how can I implement my Factory into the "Unity C#" file?

Edited question.

Here is the issue, I think it is explained enough:

This is the Factory script, which creates a singleton for my implemented interface.

 using System;
 using System.Collections;
 
 namespace Physics
 {
     public class FactoryPhysics
     {
         static Physics.iPhysics p;
         public static Physics.iPhysics getJetPhysics()
         {
             if(p == null)
                 p = new Physics.Physics_imp();
             return p;
         }
     }
 }

Now here is the Unity C# script:

 using System;
 using UnityEngine;
 using System.Collections;
 
 FactoryPhysics p = getJetPhysics();  //This line is the problem, explained below
 
 public class Jet : MonoBehaviour
 {
     // Use this for initialization
     void Start ()
     {
     rigidbody.AddRelativeForce(Vector3.forward * 200, ForceMode.Impulse);
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
     
     void FixedUpdate()
     {
         
     }
 }


The compiler says "Parser Error: Unexpected Symbol, FactoryPhysics." Although all the scripts are in the same solution.

Comment
Add comment · Show 6
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 Wolfram · Feb 04, 2013 at 10:57 AM 1
Share

Of which type is FactoryPhysics?

No offense in case I'm going to state the obvious, but:

  • All Unity objects need to derive from $$anonymous$$onoBehaviour

  • These object cannot have a Constructor, and cannot be created with "new"

  • Exclusively use AddComponent to create such an object via script

  • non-Untiy objects can be any type/class, and need a constructor

  • non-Unity-objects can be within your own namespace

  • Inheritance works for both type of objects

I believe support for namespaces has been changed/extended. Check the 4.x changelogs.

avatar image Bunny83 · Feb 04, 2013 at 11:03 AM 0
Share

I think this sentence needs a lot clarification:
"I can't create a new "FactoryPhysics" object or even use Inheritance"

avatar image Ranger-Ori · Feb 04, 2013 at 11:06 AM 0
Share

The thing is, that my FactoryObject, is actually an implemented interface, which I created so I can send information about values in the unity3D and receive values after changes I needed. The returned values are floats.

avatar image Wolfram · Feb 04, 2013 at 11:11 AM 0
Share

Both interfaces and abstract classes are possible, even for Unity objects. You'll have to explain some more, or give a code example of what doesn't work, and why it doesn't work.

avatar image Wolfram · Feb 04, 2013 at 11:13 AM 0
Share

Also, we still know nothing whatsoever what FactoryPhysics and FactoryObject are. Show us the class structure/framework/headers.

Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Wolfram · Feb 04, 2013 at 11:34 AM

Umh, add a "using Physics;" at the top, or use Physics.FactoryPhysics instead...?

Also, you are trying to call getJetPhysics outside of any class, and that line shouldn't be outside the class, too.

Comment
Add comment · Show 5 · 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 Ranger-Ori · Feb 04, 2013 at 11:40 AM 0
Share

I added using, I changed also the namespace, to NewPhysics for if there might be an encounter with unity premade physics. the new line is:

 FactoryPhysics p = FactoryPhysics.getJetPhysics();

and still, no change with the error.

avatar image Ranger-Ori · Feb 04, 2013 at 11:43 AM 0
Share

Hmm, wait, I've did the other solution you offerred, to put the singleton inside the class. Now I receive this error: Cannot convert method group 'getJetPhysics' to non-delegate FactoryPhysics.

avatar image Ranger-Ori · Feb 04, 2013 at 11:47 AM 0
Share

For the final time, I solved it thanks to you, I should've put the line inside the class and not outside. Now it works. thank you :)

the line is btw:

 iPhysics p = FactoryPhysics.getJetPhysics();
avatar image Wolfram · Feb 04, 2013 at 11:47 AM 0
Share

As you can see in your code, getJetPhysics() returns an object of type Physics.iPhysics, but you are trying to assign it to Physics.FactoryPhysics.

Ins$$anonymous$$d you should probably access your singleton as it is intended, by directly using Physics.FactoryPhysics.getJetPhysics() each time you access it, ins$$anonymous$$d of storing your static global object in another local reference.

avatar image Ranger-Ori · Feb 04, 2013 at 12:01 PM 0
Share

It could be that my design is still flawed.. since object p now stores information not only for functions, but also for variables. That's why I need to create it.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Tropical Island level design 1 Answer

Reliable / intelligent user input handling 0 Answers

Using the Delegate Object design pattern in Unity 1 Answer

2D 360 degress platformer example needed 0 Answers

Creating a singleton game manager 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